【问题标题】:Any way to avoid capture cache in Smarty 2?有什么方法可以避免在 Smarty 2 中捕获缓存?
【发布时间】:2013-02-18 18:45:22
【问题描述】:

我有一些捕获的内容多次显示在页面上,我需要防止它们被缓存,因为它第二次呈现错误的值。我了解到,在 Smarty 3 中,有新的 {nocache} 块和 nocache 标志可用于防止缓存变量,我相信在这种情况下可以帮助我。在 Smarty 2 中是否有任何替代方法可以执行相同操作,或者唯一的方法是升级到 Smarty 3 以获得此功能?

【问题讨论】:

    标签: smarty smarty2


    【解决方案1】:

    查看caching section of the Smarty2 Documentation,他们描述了如何应对这种情况的一些可能性:

    您可以使用insert 模板保持部分页面动态 功能。假设整个页面都可以缓存,除了横幅 显示在页面的下方。通过使用insert 横幅的功能,您可以在 缓存内容。

    另一种方法是在显示之前清除特定模板的缓存:

    require('Smarty.class.php');
    $smarty = new Smarty;
    ...
    $smarty->clear_cache('your_template_without_cache.tpl');
    

    【讨论】:

    • 不幸的是,我需要避免编辑 PHP 文件。我只能在 tpl 级别上执行此操作。例如。我有一个页面,在上面我通过这个 tpl 代码为每个项目呈现了一些链接:{capture "foo" assign="trfoo"}{plugin _name="include" page="item$ID"}{/plugin}{/capture} <a href="#item{$ID}" class="perex" title="{$trfoo|striptags|truncate:240}">{$itemTitle}</a> 然后我通过其他 tpl 将其他动态内容包含在页面中:{capture "foo2" assign="trfoo2"}{plugin _name="include" page="item$ID"}{/plugin}{/capture}<p class="preview">{$trfoo2|truncate:760}</p> 问题是第二个中缓存的 $ID。
    • 然后使用我回复中描述的插入模板功能。
    • 谢谢你的回答,我会试试的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2015-02-01
    • 2020-09-07
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多