Caching [cache]
Setting up caching [cache creation]
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = 2; // lifetime is per cache// set the cache_lifetime for index.tpl to 5 minutes$smarty->cache_lifetime = 300;$smarty->display('index.tpl');// set the cache_lifetime for home.tpl to 1 hour$smarty->cache_lifetime = 3600;$smarty->display('home.tpl');
// Note: The following $ cache_lifetime setting will not work when $ caching = 2. // prompt: $ chche_lifetime after $ chching = 2 does not work. // The cache lifetime for home. TPL has already been set // to 1 hour, and will no longer respect the value of $ cache_lifetime. // home. after the cache Session Period of TPL is set to one hour, the value of $ cache_lifetime is not changed. //
The home. TPL cache will still expire after 1 hour. // The cache for home. TPL will end in 1 hour. $ Smarty-> cache_lifetime = 30; // 30 seconds $ smarty-> display ('home. tpl ');
Multiple caches per page [multiple caches per page]
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;$my_cache_id = $_GET['article_id'];if(!$smarty->is_cached('index.tpl',$my_cache_id)) { // No cache available, do variable assignments here. $contents = get_database_contents(); $smarty->assign($contents);}$smarty->display('index.tpl',$my_cache_id);
Cache groups [cache set]
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;// clear all caches with "sports|basketball" as the first two cache_id groups$smarty->clear_cache(null,"sports|basketball");// clear all caches with "sports" as the first cache_id group. This would// include "sports|basketball", or "sports|(anything)|(anything)|(anything)|..."$smarty->clear_cache(null,"sports");$smarty->display('index.tpl',"sports|basketball");
Controlling cacheability of plugins 'output [control the buffer capability of the plug-in output]
Index. php:
require('Smarty.class.php');$smarty = new Smarty;$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');
Index. TPL:
Time remaining: {remain endtime = $ obj-> endtime}
Or:
Index. php:
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;function smarty_block_dynamic($param, $content, &$smarty) { return $content;}$smarty->register_block('dynamic', 'smarty_block_dynamic', false);$smarty->display('index.tpl');
Index. TPL:
Page created: {"0" | date_format: "% d % H: % m: % s "}
{Dynamic}
Now is: {"0" | date_format: "% d % H: % m: % s "}
... Do other stuff...
{/Dynamic}