kreoton web development

Archive for May, 2007

Smarty included file caching

Wednesday, May 30th, 2007

Smarty is very good templating system for every PHP application. Once I coded one application using Smarty and Apache mod rewrite. I had one template file i witch i included other template files. After some time my project had very high traffic so i needed to on cache. You probably know that Smarty does not have cache future for included file, so i googled for some time and found some useful smarty hack. I added this hack to smarty core files and wow it worked, all i had to do is just add variable to template file inclusion in example:

1
{include file='includedfile.tpl' __cache__=120}

Where __cache__ shows that i start cache for 120 second everything worked fine until i browsed to other pages. Main bug of this hack is you can not use paging on your application. To solve this problem i edited smary code and added __id__ variable to Smarty included template files. It works very simple in example:

index.php file:

1
2
3
4
5
6
7
<?php
if ($_GET['page']) {
$smarty->assign('page', $_GET['page']);
//getting dynamic content
}
$smarty->display('main.tpl');
?>

main.tpl

1
2
3
4
5
6
7
8
<html>
<head>
<title>{$pagetitle}</title>
</head>
<body>
{include file='dynamiccontent.tpl' __cache__=3600 __id__=$page}
</body>
</html>

Variable __id__ assigns in witch dynamic content page I am.

You can download hacked version of Smarty by clicking of following link http://kreoton.net/downloads/samrty.rar