Get Smarty


Donate Bitcoin Bitcoin

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from and save up to 80%.

Buy prescription glasses from and save.

Cheap Glasses Now On Sale at Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping -

Find Wedding Dresses Online at Canada from

Find your local domestic cleaner

Brautkleider auf

Find free files to download on

Looking For Affordable Wedding Dresses 2015 at Best Prices On

Shop high quality cheap prom dresses on

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store


Controlling Cacheability of Output

If caching is enabled normally the whole final output of the page gets cached. However Smarty3 offers several options how to exclude sections of your output from caching.

General Note

Be sure any variables used within a non-cached section are also assigned from PHP when the page is loaded from the cache.

Cacheability of Template Section

A larger section of your template can easily excluded from caching by using the {nocache} and {/nocache} tags.

Example 15.10. Preventing a template section from being cached

Today's date is


The above code will output the current date on a cached page.

Cacheability of Tags

Caching for an individual tag can be disabled by adding the "nocache" option flag to the tag.

Example 15.11. Preventing tag output from being cached

Today's date is
{$|date_format nocache}


Cacheability of Variables

You can assign() variables as not cachable. Any tag which uses such variable will be automatically executed in nocache mode.


If a tag is executed in nocache mode you must make sure that all other variables used by that tag are also assigned from PHP when the page is loaded from the cache.


The nocache status of an assigned variable 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.

Example 15.12. Nocache Variables

// assign $foo as nocahe variable

Dynamic time value is {$foo}


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.


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 15.13. Preventing a plugin's output from being cached


function remaining_seconds($params, $smarty) {
    $remain = $params['endtime'] - time();
    if($remain >= 0){
        return $remain . ' second(s)';
        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);



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 15.14. Preventing a whole passage of a template from being cached



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



where index.tpl is:

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


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

... do other stuff ...



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.


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.

by bordeux on May 7, 2012 at 11:08
great, but i would prefer {cache} tag.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Email: (not shown)
What is 10 plus 12? (Are you human?)


Sponsors [info]

UK Web Hosting
Best Web Hosting
Web Hosting UK
Unlimited Web Hosting
App Entwicklung
First Click Internet Marketing
Credit Card Processing Company
Cleaning Services Chicago