2011-12-20 83 views
0

我已经缓存启用我的Smarty的安装,并有以下模板函数分配值

function smarty_updatedhour($params, $smarty) 
{ 
    $date1 = new DateTime($params['timestamp']); 
    $date2 = new DateTime("now"); 
    $interval = $date1->diff($date2); 
    $smarty->assign("updated_period", $interval->format('%h'), true); 
} 

我已经注册的插件为:

$smarty->registerPlugin('function', 'updated_hour', 'smarty_updatedhour', false, array('timestamp')); 

我试图测试它是否是是否工作

{updated_hour timestamp=$timestamp_vale} 
{$updated_period} 
{if $updated_period > 10} 
    // do other stuffs 
{/if} 

但它不起作用,但是当我禁用smarty页面缓存时, 有用。

谁能告诉我是什么问题?

回答

0

在Smarty3你的插件与模板,不是Smarty对象工作。所以你的功能的足迹是function smarty_updatehour(array $params, Smarty_Internal_Template $template)。但这不是你的问题。即使是@sudhir的assignGlobal()提示也无济于事。

您的非缓存插件不能分配变量,但返回实际输出:

function smarty_updatedhour($params, $smarty) 
{ 
    $date1 = new DateTime($params['timestamp']); 
    $date2 = new DateTime("now"); 
    $interval = $date1->diff($date2); 
    return $interval->format('%h'); 
} 

,这是因为{$ updated_period}不是非缓存。该输出被评估为一次,然后写入缓存。试试{nocache}{$updated_period}{/nocache}或者使用上面修改的插件代码。

+0

好吧,我无法返回函数输出。这是因为使用{if} {/ if}在模板上对指定的值进行了进一步处理,并且实际中指定的值不用于在模板内显示。因此,从您的建议,唯一的可能性是在{nocache}中包装我的变量输出(用于测试)或{if}条件。另请注意,我在插件上分配updated_period的同时将nocache设置为true。 – Prakash 2011-12-20 17:40:57

0

尝试使用assignGlobal,如:

 
$smarty->assignGlobal("updated_period", $interval->format('%h'), true); 

希望它为你工作

+0

不,这也是行不通的。 – Prakash 2011-12-20 17:45:06