Home > php教程 > php手册 > body text

smarty模板局部缓存方法使用示例

WBOY
Release: 2016-06-13 09:30:23
Original
1194 people have browsed it

在开启smarty缓存的情况下,第一次执行时会将其编译好的输出文件保存到cache目录中,然后在程序中通过smarty的is_cache()函数检测其 cache文件是否过期,如果过期会更新缓存,如果没有过期会自动调用cache文件,这样就省去了编译的过程。检测cache过期是看模板文件是否在指定的生命周期内是否更改,这里的更改是通过检测文件的最近修改时间实现的,不是通过检测模板文件内容。


防止一个模板文件的整篇都被缓存:

index.php文件:

复制代码 代码如下:

require('smarty.class.php');
$smarty = new smarty;
$smarty->caching = true;

function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);

$smarty->display('index.tpl');


index.tpl:

复制代码 代码如下:

page created: {"0"|date_format:"%d %h:%m:%s"}

{dynamic}

now is: {"0"|date_format:"%d %h:%m:%s"}

... do other stuff ...


{/dynamic}

当重新加载这个页面,你将会注意到这两个日期不同。一个是“动态“,一个是“静态”。你能够在{dynamic}...{/dynamic}之间作任何事情,并且保证它将不会像剩下的页面一样被缓存。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!