I have caching enabled on my smarty installation and have following Template Functions
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);
}
I have registered the plugin as:
$smarty->registerPlugin('function', 'updated_hour', 'smarty_updatedhour', false, array('timestamp'));
I tried to test it whether it is working or not
{updated_hour timestamp=$timestamp_vale}
{$updated_period}
{if $updated_period > 10}
// do other stuffs
{/if}
but it does not work however when I disable the smarty page caching, it works.
can anyone tell me what is the issue?
In Smarty3 your plugins work with the template, not the smarty object. So your function’s footprint is
function smarty_updatehour(array $params, Smarty_Internal_Template $template). That’s not your problem, though. Even the assignGlobal() hint by @sudhir won’t help you one bit.your non-caching plugin cannot assign variables, but return the actual output:
that is because {$updated_period} is not non-caching. That output is evaluated EXACTLY ONCE and then written to cache. Either try
{nocache}{$updated_period}{/nocache}or go with the modified plugin code above.