Heim  >  Artikel  >  Backend-Entwicklung  >  Smarty 局部关闭缓存配置_PHP教程

Smarty 局部关闭缓存配置_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:40:081007Durchsuche

要在某些区域使缓存失效(只对需要的缓存),有几种方法:

一、inser:

定义一个inser标签要使用的处理函数,函数名格式为:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是说,如果你定义的函数为insert_abc,则模板中使用方法为{insert name=abc}

参数通过$params传入

也可以做成insert插件,文件名命名为:insert.xx.php,函数命名为:smarty_insert_aa($params,&$smarty),xx定义同上


二、register_block:

定义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名

注册block:$smarty->register_block(name, smarty_block_name, false); //第三参数false表示该区域不被缓存

模板写法:{name}内容 {/name}

写成block插件:

(1)定义一件插件函数:block.cacheless.php,放在smarty的 plugins目录

block.cacheless.php的内容如下:

function smarty_block_cacheless($param, $content, &$smarty) {

return $content;

}

?>

(2) 编写程序及模板

示例程序:testCacheLess.php

include(Smarty.class.php);

$smarty = new Smarty;

$smarty->caching=true;

$smarty->cache_lifetime = 6;

$smarty->display(cache.tpl);

?>

所用的模板:cache.tpl

已经缓存的:{$smarty.now}

{cacheless}

没有缓存的:{$smarty.now}

{/cacheless}

现在运行一下,发现是不起作用的,两行内容都被缓存了

(3)改写Smarty_Compiler.class.php(注:该文件很重要,请先备份,以在必要时恢复)

查找$this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true); //我的在705行

修改成:

if($tag_command == cacheless) $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);

你也可以直接将原句的最后一个参数改成false,我对smarty的内部机制不太了解,所以加了一个判断,只要block是cacheless的才不作缓存

(4)OK,现在清除template_c里的编译文件,重新运行,起作用了吧?

  • 来源: 编程之家
  • www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486228.htmlTechArticle要在某些区域使缓存失效(只对需要的缓存),有几种方法: 一、 inser: 定义一个inser标签要使用的处理函数,函数名格式为:insert_xx(array $params, o...
    Stellungnahme:
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn