Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Easier Smarty Inserts

Insert uncacheable content in template file

8:22 pm on Oct 24, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 3, 2007
votes: 0

Hi. I love the Smarty PHP Template Engine, but including "inserts" is just painful. Why do I need inserts? Because since I use Caching to speed up my site, EVERYTHING in my page gets cached. I need to create lots and lots of functions (Smarty Insert plugin functions) to return values of parts of the site I don't want to be cached and run on each request of that page.

I hope you know what I'm trying to say here, for Smarty users this looks simple, I imagine.

What I would like to know is if there is an easier way to insert areas such as banners, user comment areas, timestamps etc in areas of my template. I could simply create lots of smarty_insert_funcName(), but that is so repetitive, I wish there was an easier way, such as:

This area should never be cached!

Is there a way to have such a feature? Is it already built into Smarty, and I am just not aware of it? And if not, is there anything I can do to create that feature myself without having to completely abandom Smarty, which fortunately has helped me separate application logic from presentation?

Best regards,

6:57 am on Oct 25, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Jan 16, 2007
votes: 0

I don't use Smarty's caching feature, but according to the docs you can pass arguments to your insert function. So why not have one single insert function (or very few) and pass it the info to manage the insert? e.g. {insert name='banner_insert' script='insertions.php' banner='some_ad'}
{insert name='banner_insert' script='insertions.php' banner='other_ad'}
{insert name='comment_insert' script='insertions.php' article=17}
4:36 pm on Oct 28, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 3, 2007
votes: 0

cameraman, thanks for your fast reply. Unfortunately that is not exactly what I was looking for, but the good news is I found an alternative solution all by myself last night. I will share this simple solution now so that other users who have a similar problem making certain areas of their template uncacheable can use it.

Add the following function on the top of your application, before any HTML output. Usually this is inside some file with all database connection calls and function calls:

function smarty_block_dynamic($param, $content, &$smarty) {

return $content;


$smarty->register_block('dynamic', 'smarty_block_dynamic', false);

The third argument of register_block is called $cacheable and is set to false so that nothing inside that block is ever cached.

And here is your template file:


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

... do other stuff ...


Now anything in between the dynamic tag will not be cached. The entire template will be cached, but that specific area inside the dynamic tag will be recompiled on each call of the template.

I found this solution here, on the Smarty Documentation website [smarty.net].

I'm happy to have found a solution, and quite surprised at how simple it is. This will make development with templates a lot easier. Hope this helps someone. =)