日期:2014-05-17  浏览次数:20427 次

smarty自定义实现局部不缓存问题
使用插件block 法

  在Smarty/plugins目录下建一个文件block.nocache.php 内容如下:

 

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

在模板中(smarty已经配置成功):

    {nocache}  
    不缓存的内容,如:{$smarty.now}  
    {/nocache} 

按照上面的配置,nocache块包含的内容应该是时时更新的
为什么在mvc模式下使用时,不能时时更新呢?
------最佳解决方案--------------------
我记得smarty里面有个insert 是动态加载,不走缓存的吧

------其他解决方案--------------------
一、以插件形式

    指定该函数的名称,如: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);


这样问题解决,谢谢大家