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


Chapter 15. Caching

Caching is used to speed up a call to display() or fetch() by saving its output to a file. If a cached version of the call is available, that is displayed instead of regenerating the output. Caching can speed things up tremendously, especially templates with longer computation times. Since the output of display() or fetch() is cached, one cache file could conceivably be made up of several template files, config files, etc.

Since templates are dynamic, it is important to be careful what you are caching and for how long. For instance, if you are displaying the front page of your website that does not change its content very often, it might work well to cache this page for an hour or more. On the other hand, if you are displaying a page with a timetable containing new information by the minute, it would not make sense to cache this page.

Setting Up Caching

The first thing to do is enable caching by setting $caching to one of Smarty::CACHING_LIFETIME_CURRENT or Smarty::CACHING_LIFETIME_SAVED.

Example 15.1. Enabling caching

$smarty = new Smarty;

// uses the value of $smarty->cacheLifetime() to determine
// the number of seconds a cache is good for



With caching enabled, the function call to display('index.tpl') will render the template as usual, but also saves a copy of its output to a file (a cached copy) in the $cache_dir. On the next call to display('index.tpl'), the cached copy will be used instead of rendering the template again.

Technical Note

The files in the $cache_dir are named similar to the template name. Although they end in the .php extension, they are not intended to be directly executable. Do not edit these files!

Each cached page has a limited lifetime determined by $cache_lifetime. The default value is 3600 seconds, or one hour. After that time expires, the cache is regenerated. It is possible to give individual caches their own expiration time by setting $caching to Smarty::CACHING_LIFETIME_SAVED. See $cache_lifetime for more details.

Example 15.2. Setting $cache_lifetime per cache

$smarty = new Smarty;

// retain current cache lifetime for each specific display call

// set the cache_lifetime for index.tpl to 5 minutes

// set the cache_lifetime for home.tpl to 1 hour

// NOTE: the following $cache_lifetime setting will not work when $caching
// is set to Smarty::CACHING_LIFETIME_SAVED.
// The cache lifetime for home.tpl has already been set
// to 1 hour, and will no longer respect the value of $cache_lifetime.
// The home.tpl cache will still expire after 1 hour.
$smarty->setCacheLifetime(30); // 30 seconds


If $compile_check is enabled (default), every template file and config file that is involved with the cache file is checked for modification. If any of the files have been modified since the cache was generated, the cache is immediately regenerated. This is a computational overhead, so for optimum performance set $compile_check to FALSE.

Example 15.3. Disabling $compile_check

$smarty = new Smarty;




If $force_compile is enabled, the cache files will always be regenerated. This effectively disables caching, however this also seriously degrades performance. $force_compile is meant to be used for debugging purposes. The appropriate way to disable caching is to set $caching to Smarty::CACHING_OFF.

The isCached() function can be used to test if a template has a valid cache or not. If you have a cached template that requires something like a database fetch, you can use this to skip that process.

Example 15.4. Using isCached()

$smarty = new Smarty;


if(!$smarty->isCached('index.tpl')) {
    // No cache available, do variable assignments here.
    $contents = get_database_contents();



You can keep parts of a page dynamic (disable caching) with the {nocache}{/nocache} block function, the {insert} function, or by using the nocache parameter for most template functions.

Let's say the whole page can be cached except for a banner that is displayed down the side of the page. By using the {insert} function for the banner, you can keep this element dynamic within the cached content. See the documentation on {insert} for more details and examples.

You can clear all the cache files with the clearAllCache() function, or individual cache files and groups with the clearCache() function.

Example 15.5. Clearing the cache

$smarty = new Smarty;


// clear only cache for index.tpl

// clear out all cache files



No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Email: (not shown)
What is 11 plus 2? (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