Smarty模板引擎局部高级缓存技术

对不需要缓存的地方,我们可以通过insert block ,插件block 法来解决局部缓存问题

1,insert 法

定义一个函数显示时间的:

function insert_get_current_time(){
    $timestamp=empty($timestamp)?time():$timestamp;
    $timeoffset=(int) '+8';
    return $ret=gmdate("Y-n-j g:ia", $timestamp + $timeoffset * 3600);
}

然后在模板中:

{insert name="get_current_time"}


2,动态block 法

//部分缓存
function smarty_block_nocache($param, $content, $smarty)
///function smarty_block_nocache($param, $content, &$smarty) 也看到这样写的
 	{
   	  return $content;
}
$smarty->register_block('nocache', 'smarty_block_nocache', false);

在模板中:

{nocache}
{$smarty.now}
{/nocache}


3,插件block 法

在Smarty/plugins目录下建一个文件
block.nocache.php 内容如下(在php兄弟连的smarty高级缓存技术里面是用block_nocache.php,应该修正下):

<?php 
function smarty_block_nocache($param, $content, $smarty)
{
       	 return $content; 
}  
?>

然后在需要修改一个文件\libs\Smarty_Compiler.class.php  第712行

$this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true); 
 
改为  
if('uncache'==$tag_command){  
 	 $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);  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值