您的位置:首页 > 其它

smarty局部缓存技术3种方法

2011-11-21 21:37 141 查看
1,insert 法

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

Php代码

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

}

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"}

这样每次打开页面,显示的都是即时时间,而不是缓存的。注意这里的函数名一定要以insert开头,模板中的name与之对应。

这种方法简单,但是如果要显示的内容是一大块的,就不宜使用了。
2,动态block 法

Php代码

function smarty_block_nocache($param, $content, $smarty) { return $content; } $smarty->register_block('nocache', 'smarty_block_nocache', false);

function smarty_block_nocache($param, $content, $smarty)
{
return $content;
}
$smarty->register_block('nocache', 'smarty_block_nocache', false);
在模板中:

Php代码

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

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

这样每次刷新页面,显示的时间都是不同的。

3,插件block 法

在Smarty/plugins目录下建一个文件

block.nocache.php 内容如下:

Php代码

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

<?php
function smarty_block_nocache($param, $content, $smarty)
{
return $content;
}
?>
这样做与方法2的效果是一样的,模板中标签也一样。在 php文件中就不必要再register_block了,很方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: