Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

caching subtemplates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
dab
Smarty Rookie


Joined: 17 Aug 2003
Posts: 13

PostPosted: Tue Aug 19, 2003 8:28 pm    Post subject: caching subtemplates Reply with quote

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
View user's profile Send private message
datamile
Smarty Rookie


Joined: 17 May 2003
Posts: 13

PostPosted: Tue Aug 19, 2003 9:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
dab
Smarty Rookie


Joined: 17 Aug 2003
Posts: 13

PostPosted: Wed Aug 20, 2003 5:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
datamile
Smarty Rookie


Joined: 17 May 2003
Posts: 13

PostPosted: Wed Aug 20, 2003 8:18 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Aug 20, 2003 8:34 pm    Post subject: Reply with quote

write a plugin-function that fetches a cached template:

sth. like (untested, but similar stuff works, believe me Smile ):

[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
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Aug 20, 2003 9:35 pm    Post subject: Reply with quote

echo is bad!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Aug 20, 2003 9:51 pm    Post subject: Reply with quote

and while we're at it: include_php is bad! Wink
Back to top
View user's profile Send private message Send e-mail Visit poster's website
datamile
Smarty Rookie


Joined: 17 May 2003
Posts: 13

PostPosted: Fri Aug 22, 2003 7:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Aug 23, 2003 9:31 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Aug 23, 2003 10:29 pm    Post subject: Reply with quote

@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
View user's profile Send private message
dab
Smarty Rookie


Joined: 17 Aug 2003
Posts: 13

PostPosted: Sun Aug 24, 2003 9:48 am    Post subject: Reply with quote

thanks!
this was exactly what i was looking for.
Back to top
View user's profile Send private message
datamile
Smarty Rookie


Joined: 17 May 2003
Posts: 13

PostPosted: Sun Aug 24, 2003 9:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP