1、自定义函数
1)PHP文件function定义函数
中间需要注册(register_function()、register_block())
Tpl文件类似于html标签,一般情况下会写成如下格式<{title num=”10”}>
自定义函数的主要目的是增强功能也被称做自定义标签
也可成对出现――称“块”
在写法上要注意 <{title}>…..<{/title}>
版本较高的并不支持
2)当作插件写入
./plugins
如果针对的是单标记 *.block.*.php
打开该文件 function smarty_block.
扩展smarty标签功能模块,自己进行编辑
2、内建函数
Include 包含子模板 调用模板可分成三部分 单标记
子模板中也可以使用当前页声明的smarty变量
Include(“head.tpl”);
Config_load 界面的调整 单标记
<config_load file=”” section=”one”>
Capture 成对的标记 <{capture}>……<{/capture}>
$smarty.capture.
If …elseif…else /if: elseif中间不能出现空格
条件表达式不能加小括号()
= = > <
Eq gt lt ge le
Foreach ――foreach 关联数组 索引数据
From=”要循环的数组名” name=”name1”
变量:$smarty.foreach.name1.first last total etc.
Item=
Section - -for 索引数组
把关联数组重新进行保存相当于存成一个二维数组
$data[]=$row; 需要注意给本次循环指定名称name=
Name=”out” $data[out]
Cache:提高效率
*.php *.tpl
Smarty-----合并后编译(php与html混合的文件)
Templates_c
*.*
优点:省略了重新合并、编译的时间;
连接数据库不能省略
增加缓存:./cache/*.html
减少了访问数据库的次数
Display(“”,””)
$_GET[]
$_SERVER[“REQUST_URI”]
局部缓存
实时更新性要求比较高的例如登陆用户名称
欢迎amdin登陆到XXXXX
时间日期
解决步骤:自定义函数(自定义插件)
1) 指定该函数名称nocache
2) 新建文件./plugins/block.nocache.php
内容:
<?php
function smarty_block_nocache($args, $content){
return $content;}
?>
3)*.php
….
$tpl->assign(“date,date(“H:i:s”));
….
*.tpl
<{nocache}><{$date}><{/nocache}>
所有插件默认被缓存
Smarty. 712 行
Else{
if($tag_command==nocache){
$this->_plugins['block'][$tag_command]= array($plugin_func, null, null, null, false);}
Else{
$this->_plugins['block'][$tag_command]= array($plugin_func, null, null, null, true);
}
}
第二种:php文件内自定义函数并注册
*.php
$tpl->register_block(“nocache”,”fun1”,false);
function fun1($args,$content){return$content;}
$tpl->assign(“date”,date(“H:i:s”));
*.tpl
<{nocache}><{$date}><{/nocache}>
第三种:smarty内建函数insert
定义一个inser标签要使用的处理函数
函数名格式为:
insert_xx(array $params, object &$smarty)
其中的xx是insert的name,也就是说,如果你定义的函数为insert_abc,则模板中使用方法为{insert name=abc}参数通过
$params传入
也可以做成insert插件,文件名命名为:insert.xx.php,函数命名为:smarty_insert_aa($params,&$smarty),xx定义同上