Caching with Smarty and dynamic blocks

Today I wanted to enable Caching for my smarty-driven, at least for the front page, because this is the page with most pageviews and due to some heavy db-queries reponse times sometimes where more than 3 seconds, which is bad.

Setting up caching for smarty is pretty straightforward:

[geshi lang=php]
@require(SMARTY_DIR . „Smarty.class.php“);

global $smarty;
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;

// CallBack function for the dynamic block
function smarty_block_dynamic($param, $content, &$smarty) {
return $content;

/** here we define a new tag {dynamic} where we can define areas, which are not beeing cached **/
$smarty->register_block(‚dynamic‘, ’smarty_block_dynamic‘, false);

As this application is using different modules like a „portal-page-module“ and in this particular case, I just want to enable caching for the front page, I will setup the cache on the lowest level, directly in the specific modules pages, because I want to have control over every cached page. In contrast to this approach, you can specify the caching parameters in a global place like inc_templates.php, if you want to have the same caching behaviour for every page.

Here is what I do in the startpage which should be cached:


[geshi lang=php]
$smarty->caching = 2; // lifetime is per cache
$smarty->cache_dir = TEMPLATE_ROOT . „cache“;
// set the cache_lifetime to 30 minutes
$smarty->cache_lifetime = 1800;

if(!$smarty->is_cached(„home_portal.tpl“, „home_portal“)) {
echo „„; // just output html for debugging

/**do the normal stuff like queries etc… **/

echo „„; // just output html for debugging

$content = $smarty->fetch(„home_portal.tpl“, „home_portal“);

Dynamic Block definition in the template tpl files

You remember the dynamic block tag we defined ealier {dynamic}

in the head of the page, we have an area with a login box. we don’t want to cache this area of the page, because when a user is logged in, this area should change and show e.g. „User logged in as Username“ and is different for every user.
That is why we sourround this area by our new defined {dynamic} tag

Example header.tpl:

[geshi lang=php]
{if isset($user_data) && count($user_data) > 0}
You are logged in as USERNAME. Goto my MyUserArea
Show a loginbox with username and password here.

Doing this we achieve that this part of the page is never beeing cached.


Setting up a simple cache is really easy with smarty.
Problems will occur, if I want to add caching for other pages too, because I already tried that, but I had some problems, because some of the cached files were overwritten by each other, because they use the same filenames.
So I will have to find to right place in the application, where to initialize the caching.
Maybe I will try to use a different cache directory for every module/page, but this is another story and will be covered in another article probably. So far „it got the job done…“ 🙂

Dieser Beitrag wurde unter Allgemein, Software-Development veröffentlicht. Setze ein Lesezeichen auf den Permalink.

2 Antworten auf Caching with Smarty and dynamic blocks

  1. Pingback: machine a sous

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.