需要技巧来在 nocache 部分使用缓存值
我想迭代单个数组并仅对某些元素禁用缓存。 所以我的想法是在 nocache 部分保留 key
并通过 key
获取元素。不幸的是,我还没有发现任何可能性:
在 nocache 部分分配缓存的 $rec@key
,
或者在缓存部分保留变量 key
定义。
有什么方法(无需修改智能代码)可以做到这一点吗? 这里是我的 test.tpl:
{foreach $array as $rec}
{if $rec.dynamic}
{assign var="key" value=$rec@key}
{nocache}
{$array[$key].text}
{/nocache}
{else}
{$rec.text}
{/if}
{/foreach}
和 test.php:
<?php
include_once 'libs/Smarty.class.php';
$smarty=new smarty();
$smarty->caching=1;
$smarty->assign('array',array(
'r1'=>array('dynamic'=>true,'text'=>'dynamic'),
'r2'=>array('dynamic'=>false,'text'=>'static')
));
$smarty->display('test.tpl');
(当然我会用它来做比文本显示更复杂的事情:)) 我尝试了很多技巧,但我自己认为这是不可能的,请告诉我我错了:)
I want to iterate thru single array and disable caching only for some elements.
So my idea was to keep key
and get element by key
in nocache section. Unfortunatelly i haven't found any possibility to:
assign cached $rec@key
in nocache section,
or keep variable key
definition in cached section.
Is there any way (without smarty code modification) to do it ?
here my test.tpl:
{foreach $array as $rec}
{if $rec.dynamic}
{assign var="key" value=$rec@key}
{nocache}
{$array[$key].text}
{/nocache}
{else}
{$rec.text}
{/if}
{/foreach}
and test.php:
<?php
include_once 'libs/Smarty.class.php';
$smarty=new smarty();
$smarty->caching=1;
$smarty->assign('array',array(
'r1'=>array('dynamic'=>true,'text'=>'dynamic'),
'r2'=>array('dynamic'=>false,'text'=>'static')
));
$smarty->display('test.tpl');
(of course i will use it for much more complicated things than text display:) )
I tried lot of tricks and by myself i think it is not possible, please tell me i am wrong :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,我找到了一个解决方案:在循环中使用 count 。
Finally, I have found a solution: Use count in your loop.
从 Smarty 3.1.x 开始,您想要完成的任务是不可能的。在 3.2 中,Smarty 将允许您将值“导出”到 {nocache} 部分,以确保它们在模板重新执行时可用。
在 3.2 发布之前(不要问日期,我不知道),您也许可以使用编译器函数自己完成此操作。
What you are trying to accomplish is, as of Smarty 3.1.x, not possible. With 3.2 Smarty will allow you to "export" values into a {nocache} section to ensure they're available when the template is re-executed.
Until 3.2 is released (don't ask for a date, I don't know) you may be able to do this yourself using a compiler function.