今天老师讲到了Smarty模板引擎的局部缓存,感觉挺好的,下面是三种方法实现局部缓存:
1、 php文件中自定义函数并注册:
php文件中:
//自定义一个函数
<?php
function fun1($args,$content){
return $content;
}
//注册函数
$tpl->register_block(“nocache”,”fun1”,false);
//分配变量
$tpl->assign(“date”,date(“H:i:s”));
?>
模板文件中:
<{nocache}><{$date}></nocache>
2、 插件形式:
在插件文件夹plugins下创建插件block.nocache.php:
<?php
function smarty_block_nocache($args,$content){
return $content;
}
?>
执行文件中:
<?php
//分配变量
$tpl->assign(“date”,date(“H:i:s”));
?>
模板文件中:
<{nocache}><{$date}><{/nocache}>
3、 使用内建函数insert:
执行文件:
<?php
//定义一个函数显示时间
function insert_get_current_time(){
$time=time();
return $time;
}
?>
模板文件中:
<{insert name=”get_current_time”}>
这样从数据库获取的就是缓存的,而其中上面的代码显示的时间是更新的。