一、以插件形式
指定该函数的名称,如:nocache。然后到plugins这个文件下建立块函数。(./plugins/block.nocache.php)命名文件的名字的时候要遵循自定义函数的要求。我们要在这个函数里把每次请求出来的部分内容显示出来,不让它生成缓存:
<?php
function smarty_block_nocache($args, $content){
return $content;}
?>
在smarty里面所有的插件默认被缓存。所以这个文件也会被缓存。这时我们要修改Smarty_Compiler.class.php这个配置文件,在文件的712行的位置上会:“ $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true); 。”这里把括号里面的true改成false,这样所有的插件就不会被自动生成缓存。如果我们不想把所有的插件都改成不被缓存的状态,只想我把写的block.nocache.php这个文件不被缓存。
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:
<?php
//本文件使用模版类
//首先包含该模板文件
include("init.inc.php");
//注册函数
$tpl->register_function("data","fun1");
//定义fun1函数
function smarty_block_nocache($args, $content){return $content;}
//分配变量
$tpl->assign("data",date("H:i:s"));
//调用模板文件
$tpl->display("a.html");
?>
* . tpl:
<{nocache}><{$date}><{/nocache}>