<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Controlling Cacheability of Plugins' Output</title> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Smarty Manual"> <link rel="up" href="caching.html" title="Chapter 14. Caching"> <link rel="prev" href="caching.groups.html" title="Cache Groups"> <link rel="next" href="advanced.features.html" title="Chapter 15. Advanced Features"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Controlling Cacheability of Plugins' Output</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="caching.groups.html">Prev</a> </td> <th width="60%" align="center">Chapter 14. Caching</th> <td width="20%" align="right"> <a accesskey="n" href="advanced.features.html">Next</a> </td> </tr> </table> <hr> </div> <div class="sect1" title="Controlling Cacheability of Plugins' Output"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="caching.cacheable"></a>Controlling Cacheability of Plugins' Output</h2></div></div></div> <p> Since Smarty-2.6.0 plugins the cacheability of plugins can be declared when registering them. The third parameter to <a class="link" href="api.register.block.html" title="register_block()"><code class="varname">register_block()</code></a>, <a class="link" href="api.register.compiler.function.html" title="register_compiler_function()"> <code class="varname">register_compiler_function()</code></a> and <a class="link" href="api.register.function.html" title="register_function()"><code class="varname">register_function()</code></a> is called <em class="parameter"><code>$cacheable</code></em> and defaults to <code class="constant">TRUE</code> which is also the behaviour of plugins in Smarty versions before 2.6.0 </p> <p> When registering a plugin with <code class="literal">$cacheable=false</code> 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 <a class="link" href="plugins.inserts.html" title="Inserts"><code class="varname">{insert}</code></a> function. </p> <p> In contrast to <a class="link" href="plugins.inserts.html" title="Inserts"><code class="varname">{insert}</code> </a> the attributes to the plugins are not cached by default. They can be declared to be cached with the fourth parameter <em class="parameter"><code>$cache_attrs</code></em>. <em class="parameter"><code>$cache_attrs</code></em> 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. </p> <div class="example"> <a name="id3118759"></a><p class="title"><b>Example 14.10. Preventing a plugin's output from being cached</b></p> <div class="example-contents"> <pre class="programlisting"> <?php $smarty->caching = true; function remaining_seconds($params, &$smarty) { $remain = $params['endtime'] - time(); if($remain >= 0){ return $remain . ' second(s)'; }else{ return 'done'; } } $smarty->register_function('remaining', 'remaining_seconds', false, array('endtime')); if (!$smarty->is_cached('index.tpl')) { // fetch $obj from db and assign... $smarty->assign_by_ref('obj', $obj); } $smarty->display('index.tpl'); ?> </pre> <p> where <code class="filename">index.tpl</code> is: </p> <pre class="programlisting"> Time Remaining: {remaining endtime=$obj->endtime} </pre> <p> The number of seconds till the endtime of <code class="literal">$obj</code> 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. </p> </div> </div> <br class="example-break"><div class="example"> <a name="id3118821"></a><p class="title"><b>Example 14.11. Preventing a whole passage of a template from being cached</b></p> <div class="example-contents"> <pre class="programlisting"> index.php: <?php $smarty->caching = 1; function smarty_block_dynamic($param, $content, &$smarty) { return $content; } $smarty->register_block('dynamic', 'smarty_block_dynamic', false); $smarty->display('index.tpl'); ?> </pre> <p> where <code class="filename">index.tpl</code> is: </p> <pre class="programlisting"> Page created: {'0'|date_format:'%D %H:%M:%S'} {dynamic} Now is: {'0'|date_format:'%D %H:%M:%S'} ... do other stuff ... {/dynamic} </pre> </div> </div> <br class="example-break"><p> When reloading the page you will notice that both dates differ. One is <span class="quote">“<span class="quote">dynamic</span>”</span> one is <span class="quote">“<span class="quote">static</span>”</span>. You can do everything between <code class="literal">{dynamic}...{/dynamic}</code> and be sure it will not be cached like the rest of the page. </p> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="caching.groups.html">Prev</a> </td> <td width="20%" align="center"><a accesskey="u" href="caching.html">Up</a></td> <td width="40%" align="right"> <a accesskey="n" href="advanced.features.html">Next</a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Cache Groups </td> <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td> <td width="40%" align="right" valign="top"> Chapter 15. Advanced Features</td> </tr> </table> </div> </body> </html>