基本配置
$smarty = new Smarty();
//左定界符
$smarty->left_delimiter = "{";
//右定界符
$smarty->right_delimiter = "}";
//是否使用缓存,开发阶段不建议使用缓存,所以~false
// $smarty->caching = false;
//模板目录,就是保存模板的位置,写对了smarty才能找到 html的模板位置
$smarty->template_dir = './tpl';
//编译目录,保存编译后的tpl的地方 模板编译生成的文件
$smarty->compile_dir = './tpl_c';
//外部配置文件目录,这个配置文件我个人理解是放一些Smarty相关的常量的,之后会说。
$smarty->config_dir = './configs';
//开启缓存
$smarty->caching = true;
//缓存目录
$smarty->cache_dir = "./cache";
//缓存时间
$smarty->cache_lifetime = 120;
常用的两个方法
- assign
// 第一个参数是变量名,第二个参数是参数的值
$smarty->assign("time", time());
- display
// 调用哪个模板文件显示
$smarty->display("../tpl/test.tpl");
基本语法
- 输出基本变量
{$articleTitle}
- 输出一维数组
{$arr['title'] } or { $arr.title}
- 输出二维数组
// array("articleContent"=>array("title"=>"学习","auhor"=>"lzq"));
{$arr['articleContent'].title}
变量调节器
- 1.首字母大写 capitalize
实例:{$articleTitle|capitalize}
- 2.字符串连接 cat
实例:{$articleTitle|cat : " 链接的字符串 " : "多个参数用冒号链接"}
- 3.日期格式化 date_format
实例:{$time|date_format}
{$time|date_format : " %A, %B, %e, %Y %H:%M:%S "}
- 4.为未赋值或为空的变量指定默认值default
{$articleTiele|default : "no title"}
- 5.转码 escape
用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转吗,十六进制美化,或者javascript转码。默认是html转码
- 6.小写lower 大写 upper (将变量字符串变小(大)写)
实例:{
articleTitle|upper}
- 7.所有的换行符将被换成
nl2br 功能同php的nl2br()的函数一样
实例:{$articleTitle|nl2br}
条件判断
条件修饰符 eq(==) neq(!=) gt(>) lt(<)
修饰符必须和常量或者变量用空格隔开
- 基本句式
{if $name eq "Mr"}
Welcome Sir.
{elseif $name eq "Mrs"}
Welcome Madam.
{else}
Welcome {$name}
{/if}
循环语句
{* section循环函数 *}
{* name是作为一个循环的id值 *}
{section name=article loop=$articlelist}
{$articlelist[article].title}
<br>
{/section}
{$hr}
{* foreach循环函数 *}
{foreach $articlelist as $article}
{if $article@index == 1}
{break}
{/if}
{$article.title}
<br>
{foreachelse}
当前没有文章
{/foreach}
文件引用
{* 文件引用,include中的属性会传递给被引入的文件,在引入文件中是不能使用的 *}
{include file="./header.tpl" title="标题" sitename="网站名称"}
对象赋值与使用
{* 对象赋值到模板中 *}
{$obj->method(array("苹果","熟了"))}
smarty中使用php内置函数
{* 在smarty使用php内置函数 *}
{"Y-m-d"|date:$time}
{$br}
{"strong"|str_replace:"handsome boy":$str}
注册自定义函数
// 先在php文件中注册函数
// f-test是注册变量名 test是需要注册的函数名
$smarty->registerPlugin("function","f_test","test");
// 再在smarty模板文件中使用上自定义函数
// 第一个是注册的函数名字 后面跟着的两个参数是代表函数的属性和值,在编译的时候会打包成一个数组,赋值给$params;
{f_test p1="香蕉" p2="熟了"}
自定义插件
- 1.function
// 先按照规定写好函数体
function smarty_function_test($params)
{
$width = $params['width'];
$height = $params['height'];
$area = $width * $height;
return $area;
}
// 通过模板文件调用smarty函数
{test width=2200 height=400}
- 2.modifier (类似于变量调节器的写法)
// 先按照规定写好变量调节器插件
function smarty_modifier_test($utime, $format)
{
return date($format, $utime);
}
// 通过模板文件调用smarty变量调节器插件
{$time|test:"Y-m-d H:i:s"}
- 3.block(类似于foreach的用法,用两个闭合标签组合)
// 先按照规定写好区块函数插件的方法
function smarty_block_blockTest($params, $content)
{
$replace = $params['replace'];
$maxnum = $params['maxnum'];
if ($replace == 'true') {
$content = str_replace(",", ",", $content);
$content = str_replace("。", ".", $content);
}
$content = substr($content, 0, $maxnum);
return $content;
}
// 通过模板文件调用smarty区块函数插件
{blockTest replace="true" maxnum=200}
{$str}
{/blockTest}
网友评论