View previous topic :: View next topic |
Author |
Message |
dab Smarty Rookie
Joined: 17 Aug 2003 Posts: 13
|
Posted: Tue Aug 19, 2003 8:28 pm Post subject: caching subtemplates |
|
|
Hi
i'm using subtemplates that i want to be cached. I have a simular setup:
index.tpl (not cached)
poll.tpl (cached)
for performance reasons i'd like the poll to be cached, and later cleared will a vote is done or a new poll is created.
but it just seems as the index.tpl is being cached. Any ideas or pointers?
thanks in advance.
/dab |
|
Back to top |
|
datamile Smarty Rookie
Joined: 17 May 2003 Posts: 13
|
Posted: Tue Aug 19, 2003 9:24 pm Post subject: |
|
|
I used include_php for sub templates , and then you can control the caching of caching of each of them,. The main template is not cached.
By naming the cache file , or using cache groups you can then clear the poll cache when the poll changes |
|
Back to top |
|
dab Smarty Rookie
Joined: 17 Aug 2003 Posts: 13
|
Posted: Wed Aug 20, 2003 5:45 pm Post subject: |
|
|
thanks for the reply
i rather not use php_include for security reasons. isn't there another way to do it?
could you show me an example of what you mean? Maybe i can write a plugin that does the same thing? |
|
Back to top |
|
datamile Smarty Rookie
Joined: 17 May 2003 Posts: 13
|
Posted: Wed Aug 20, 2003 8:18 pm Post subject: |
|
|
http://autodemo.webmarketeur.com/
is using them..... all the block areas are include_php templates
Perhaps there is a difference way of doing it , but it worked for me |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Aug 20, 2003 8:34 pm Post subject: |
|
|
write a plugin-function that fetches a cached template:
sth. like (untested, but similar stuff works, believe me ):
[php:1:33a2d75dec]
function smarty_function_include_cached($params, &$smarty) {
$caching = $smarty->caching;
$smarty->caching = true;
$content = $smarty->fetch($params['file'], $params['cache_id']);
$smarty->caching = $caching;
return $content;
}
[/php:1:33a2d75dec]
(it may be even faster to call display() instead of fetch here, but *returning* the output is the correct way for plugins and *echoing* is deprecated. judge yourself.)
Code: |
{include_cached file="poll.tpl" cache_id=$assigned_cache_id}
|
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Aug 20, 2003 9:35 pm Post subject: |
|
|
echo is bad! |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Aug 20, 2003 9:51 pm Post subject: |
|
|
and while we're at it: include_php is bad! |
|
Back to top |
|
datamile Smarty Rookie
Joined: 17 May 2003 Posts: 13
|
Posted: Fri Aug 22, 2003 7:21 pm Post subject: |
|
|
Ok mr admins....
Is that the perferred solution instead of include_php ? I can see some benefits of having accesses to smarty and global vars , but others with that its nolonger running in isolation like include_php.
Would you have to add all the code to do the sub-template specific stuff etc ?
Got a bigger example ? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Aug 23, 2003 9:31 pm Post subject: |
|
|
i think it's a matter of taste. to me {include_php} and {php}...{/php} always smell like the wrong kind of logic being put into the template. when i encounter the need for that i always first ponder what can i do in php before display() to avoid php in the template. if nothing helps i write a plugin but i never put php-code directly in the template.
you can use an array as $smarty->plugins_dir so you don't have to put the plugins in the smarty-installation directory (and IMHO never should), but can maintain a project-specific plugins_dir next to the smarty distribution's plugins_dir. this makes upgrading or switching versions of smarty and maintainance really easy even with your additional plugins.
that's the way i do it and i have good experience with it, but AFAIK there is no official proposal to do it this or that way. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Aug 23, 2003 10:29 pm Post subject: |
|
|
@messju: well said! A user created template really has no business doing any of the PHP work--or other types of heavy lifting. php_include is a good case in point. With php_include, the sentiment is: "this template relies on this functionality". Almost certainly, that functionality is going to be something that loads in (or massages) some data structure for the template. That is precisely the sort of thing that should be done prior to display() or if really necessary, as a plugin.
Another point on multiple plugin dirs using an array: The first plugin dirs are searched in order for the plugin being called. That means that if you put your plugin dir before smarty's plugin dir, you can effectively override existing plugins without having to change anything in the distribution code.
On the other hand, if using multiple dirs, you should attempt to put the most often used plugins in the first dir that will be checked to maximize performance (otherwise, smarty has to search each path). |
|
Back to top |
|
dab Smarty Rookie
Joined: 17 Aug 2003 Posts: 13
|
Posted: Sun Aug 24, 2003 9:48 am Post subject: |
|
|
thanks!
this was exactly what i was looking for. |
|
Back to top |
|
datamile Smarty Rookie
Joined: 17 May 2003 Posts: 13
|
Posted: Sun Aug 24, 2003 9:31 pm Post subject: |
|
|
I kind of view it round the other way.
All the sub-templates are php-modules which are all independant, and the master template decided which sub-template to include.
Each to their own , but I will look at the other way too |
|
Back to top |
|
|