PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Cacheability of Plugins

The cacheability of plugins can be declared when registering them. The third parameter to registerPlugin() is called $cacheable and defaults to TRUE.

When registering a plugin with $cacheable=false the plugin is called everytime the page is displayed, even if the page comes from the cache. The plugin function behaves a little like an {insert} function.

Note

The $cacheable status will effect the compiled template code. If you change the status you must manually delete existing compiled and cached template files to force a recompile.

In contrast to {insert} the attributes to the plugins are not cached by default. They can be declared to be cached with the fourth parameter $cache_attrs. $cache_attrs is an array of attribute-names that should be cached, so the plugin-function get value as it was the time the page was written to cache everytime it is fetched from the cache.

Example 204. Preventing a plugin's output from being cached



<?php
$smarty
->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

function 
remaining_seconds($params$smarty) {
    
$remain $params['endtime'] - time();
    if(
$remain >= 0){
        return 
$remain ' second(s)';
    }else{
        return 
'done';
    }
}

$smarty->registerPlugin('function','remaining''remaining_seconds'false, array('endtime'));

if (!
$smarty->isCached('index.tpl')) {
    
// fetch $obj from db and assign...
    
$smarty->assignByRef('obj'$obj);
}

$smarty->display('index.tpl');
?>

   

where index.tpl is:


Time Remaining: {remaining endtime=$obj->endtime}

   

The number of seconds till the endtime of $obj is reached changes on each display of the page, even if the page is cached. Since the endtime attribute is cached the object only has to be pulled from the database when page is written to the cache but not on subsequent requests of the page.


Example 205. Preventing a whole passage of a template from being cached



index.php:

<?php
$smarty
->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

function 
smarty_block_dynamic($param$content$smarty) {
    return 
$content;
}
$smarty->registerPlugin('block','dynamic''smarty_block_dynamic'false);

$smarty->display('index.tpl');
?>

   

where index.tpl is:


Page created: {'0'|date_format:'%D %H:%M:%S'}

{dynamic}

Now is: {'0'|date_format:'%D %H:%M:%S'}

... do other stuff ...

{/dynamic}

   

When reloading the page you will notice that both dates differ. One is “dynamic” one is “static”. You can do everything between {dynamic}...{/dynamic} and be sure it will not be cached like the rest of the page.

Note

The above example shall just demonstrate how a dynamic block plugins works. See Cacheability of Template Section on how to disable caching of a template section by the built-in {nocache} and {/nocache} tags.

Smarty Template Engine