Question about caching (2) - now: caching with include's

From: Date: Tue, 23 Sep 2003 09:16:03 +0000
Subject: Question about caching (2) - now: caching with include's
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-10258@lists.php.net to get a copy of this message
Hello all, Since I didn't get a response on my previous mail ('Question about caching' on 20/09), I tried to figure something out. Actually my question was: how do you deal with caching if you have a lot of includes, and only parts of the includes should get cached, and not the whole site? Imagine you have the following template for showing a news site: news.html: {section name=news loop=$newsArray} {include file="news-item.html" newsItem=$newsArray[news]}<br><br> {/section} news-item.html, included by news.html: {$newsItem.id} - {$newsItem.title} Now I wanted to cache the newsitem.html-files, so that I can cache all the news-items separately. The cool thing about this is that if one news-item changes, I don't have to delete the whole news cache, but I can simple delete the cache results of one news-item and leave the others unchanged! So instead of caching news.html, I want to cache several newsitem.html's! (divide et impera!) In my first thought, this was nearly impossible, because in this situation you have to handle the caching stuff in the template, instead of the php files. Ofcourse, you can enable caching on news.html, make a block function which disables caching on news.html, except on the included newsitem.html files. This might work, but this is 1) not a good solution 2) you might have problems with declaring the cache id's for the newsitem.html files, to make each cached newsitem.html unique! So I tried to make a smarty template function: function smarty_function_myCache($params, &$smarty) { $smarty->caching = 2; $smarty->cache_lifetime = 99999999; if (!$smarty->is_cached($params['file'], $params['id'])) $smarty->assign($params['var'], ($params['data'])); /* should be array, fix me! */ $returnData = $smarty->fetch($params['file'], $params['id']); $smarty->caching = 0; return $returnData; } Now my template files look like this: news-2.html: {section name=news loop=$newsArray} {myCache file="news-item-2.html" id=$newsArray[news].id var="newsItem" data=$newsArray[news]}<br><br> {/section} ewsitem-2.html = newsitem.html: {$newsItem.id} - {$newsItem.title} This works! So now every newsitem.html file is cached 'forever', unless it gets deleted because a user changed it. (I do this in my php files: clear_cache('newsitem-2.html', "NEWS" . $id)) BUT I think this is not a good way of programming, because now the template designed have to decide how something gets cached, and in my opinion this is not the task of the template designer? Even it is, and the template designer does something wrong (for example declaring a wrong id, as I did here: I declared $id, instead of NEWS$id), the php files won't notice it, and bad things can happen! On the other hand, the template designer isn't 'free' anymore: he MUST separate news and newsitems, he MUST call it newsitem-2.html, he MUST.... in order to let caching working properly. So I'd like to know your opinion about this situation, and how you would do it? This is ofcourse a simple example, but imagine you have more complex site with lots of includes! In my site, I always parse an 'index.tpl' file, which includes the proper section of the site, with {include}. I did this, because I wanted to give the template designer some freedom about structuring his site. Thanks for your input, Peter

« previous php.smarty.general (#10258) next »