Smarty模板

2018-10-20  本文已影响33人  duans_

PHP模板引擎

该文档使用的是Smarty 3.1.33 Released版本

什么是PHP模板引擎?

什么是Smarty?

Smarty的配置

smart配置左右定界符

$smarty = new Smarty();
$smarty->left_delimiter = '{{';
$smarty->right_delimiter      = '}}';

smarty自定义模板文件目录

//设置模板文件目录
$smarty->setTemplateDir($dir) 
//$smarty->getTemplateDir();$template_dir;
// 设置编译目录
$smarty->setCompileDir('./templates_c');
//$smarty->getCompleDir();$compile_dir;
// 设置缓存目录
$smarty->setCacheDir('./cache');
//$smarty->getCacheDir();$cache_dir;

// 方法连用
$smarty->setTemplateDir('./templates')
       ->setCompileDir('./templates_c')
       ->setCacheDir('./cache');

Smarty的使用

内置函数(模板页面常用标签)

{foreach}

  {$data = [1,2,3,4,5]}
  {foreach $data as $value}
    {$key}-{$value}
  {/foreach}

{php}

{php}
$arr=array('name'=>'zs','age'=>19);
foreach($arr as $key=>$val){
    echo $key.'='.$val;
}
{/php}

{include}

{if} {elseif} {else}

{if isset($name) && $name == 'Blog'}
     {* do something *}
{elseif $name == $foo}
    {* do something *}
{/if}

{if is_array($foo) && count($foo) > 0}
    {* do a foreach loop *}
{/if}

{literal}

<script>
   {literal}
     function myBar {alert('Bar!');}
   {/literal}
</script>

{**}

{*这是一段smarty注释*}

{assign}

{assign var="name" value="Bob"}
//{$name}会输出'Bob'
{$name}

变量修饰符

常用系统变量修饰符

default
{$name|default='暂无姓名'}
truncate
{assign var='title' value='这是一个很长的Title,长到会撑破你的网页结构,你怎么办?'}
//这是一个很长的T
{$title|truncate:8:}
//这是一个很长的标T...
{$title|truncate:8:"..."}
//第三个参数表示按照单词截取,按照指定长度截取的同时,会保证单词的完整性不被破坏
//这是一个很长的标Title...
{$title|truncate:8:"...":true}
strip_tags
{assign var='content' value='<h1>我是h1标题</h1>'}
{$articleTitle|strip_tags}
nl2br
//php页面
<?php
$smarty->assign('title',
                "Sun or rain expected\ntoday, dark tonight"
                );
?>
//模板页面
{$title|nl2br}
lower
{assign var='title' value="THIS IS A TITLE"}
{$title|lower}
upper
{assign var='title' value="this is a title"}
{$title|upper}

自定义变量修饰符

{assign var='title' value='This is as title'}
//全部转成大写
{$title|strtoupper}
//全部转成小写
{$title|strtolower}
//计算字符串长度
{$title|strlen}

自定义函数(了解)

html_options

<?php
$smarty->assign('myOptions', array(
                                1800 => 'Joe Schmoe',
                                9904 => 'Jack Smith',
                                2003 => 'Charlie Brown')
                                );
$smarty->assign('mySelect', 9904);
?>
/*
    name:为select标签最终的name
    options:下拉框的选项
    selected:默认选中项
*/ 
{html_options name=foo options=$myOptions selected=$mySelect}
<select name="foo">
<option value="1800">Joe Schmoe</option>
<option value="9904" selected="selected">Jack Smith</option>
<option value="2003">Charlie Brown</option>
</select>

{html_table}

<?php
$smarty->assign( 'data', array(1,2,3,4,5,6,7,8,9) );
$smarty->assign( 'tr', array('bgcolor="#eeeeee"','bgcolor="#dddddd"') );
$smarty->display('index.tpl');
?>
{html_table loop=$data cols="first,second,third,fourth" tr_attr=$tr}
<table border="1">
<thead>
<tr>
<th>first</th><th>second</th><th>third</th><th>fourth</th>
</tr>
</thead>
<tbody>
<tr bgcolor="#eeeeee"><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr bgcolor="#dddddd"><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr bgcolor="#eeeeee"><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</tbody>
</table>

fetch

{* assign the fetched contents to a template variable *}
{fetch file='http://www.baidu.com/' assign='weather'}
<div id="weather">{$weather}</div>

缓存(了解)

开启缓存

<?php
require('Smarty.class.php');
$smarty = new Smarty;

// 使用$smarty->cacheLifetime()可以更精确定义缓存时间

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

$smarty->display('index.tpl');
?>

设置缓存时间

<?php
require('Smarty.class.php');
$smarty = new Smarty;

// 让每个缓存的过期时间都可以在display执行前单独设置。
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

// 设置index.tpl的过期时间为5分钟
$smarty->setCacheLifetime(300);
$smarty->display('index.tpl');

// 设置home.tpl的过期时间为1小时
$smarty->setCacheLifetime(3600);
$smarty->display('home.tpl');

// 注意:当$caching设置了Smarty::CACHING_LIFETIME_SAVED后,
// 下面的$cache_lifetime将不会起效。
// home.tpl已经设置了过期时间为1小时,
// 所以不会再遵循下面的$cache_lifetime值,
// home.tpl的过期时间还是1小时。
$smarty->setCacheLifetime(30); // 30 秒
$smarty->display('home.tpl');
?>

删除缓存

<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

if(!$smarty->isCached('index.tpl')) {
    // 找不到缓存,这里进行一些赋值操作
    $contents = get_database_contents();
    $smarty->assign($contents);
}

$smarty->display('index.tpl');
?>

附录

Smarty官网地址
Smarty官方文档

上一篇 下一篇

猜你喜欢

热点阅读