smarty實現局部不緩存有兩種方法:
一、以插件形式
指定該函數的名稱,如: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文件:a.php
a . 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("b.html");
?>
新建一個html模板文件:b.html
b . html:
//輸出日期測試一下
<{nocache}><{$date}><{/nocache}>