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

Isolate Caching in included files

 
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
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Thu Apr 24, 2003 12:19 am    Post subject: Isolate Caching in included files Reply with quote

Is Smarty able to allow me to control caching of a section of a template (or an included template) within another template?

For example, most of our page should be cached, as it changes very little. However the top header navigation has login/logout info or "hi (name)" depending on whether the user is logged in or out. It is a relatively small part of the page.

Obviously we could design it so that the php file calling the template constructs several smarties, all with different caching, but this would complicate our code, as the page composition should reside in the template and not in the php. It would be great if there were a '{nocache}' tag, or perhaps if I could include a file with a modifier like: {include file="nav/Nav_Top.tpl" cache="no"}

maybe this belongs in the new features section??
Back to top
View user's profile Send private message Visit poster's website
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Thu Apr 24, 2003 12:26 am    Post subject: insert Reply with quote

I just saw the insert function--it was a bit misleading because of the name but now it seems that is what I was looking for.

However the documentation is confounding. It tantalizes me by saying it works like an include. However it seems to work nothing at all like an include and there are no examples of this. Does anyone have one?
Back to top
View user's profile Send private message Visit poster's website
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Thu Apr 24, 2003 4:41 pm    Post subject: Solution found (?) Reply with quote

Smarty, as always, came through with a solution that is exremely elegant. But If anyone is out there who can confirm this I would appreciate it.

I put this function in my smarty.inc that is included in every page to create the customized smarty class for the site:
function insert_noCacheTpl($params, &$smarty){
$smarty->caching=false;
return($smarty->fetch($params['tpl']));
$smarty->caching=$CACHING;
}
note: the $CACHING is a constant inherited from the top-level include that allows us to globally turn all caching off, for instance on development servers. Of course it would need to store the value true. Perhaps this last line is not needed, I'm not sure.

I then put this in my "top nav" template:
{insert name="noCacheTpl" tpl="nav/SessionLinks.tpl"}

Then I created a template for the user-specific links that should not be cached.

This solution appears to be working. Does anyone see a problem with it, or a way to simplify it further?

thanks
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Apr 24, 2003 4:46 pm    Post subject: Reply with quote

I don't think you'll never reach the line:

$smarty->caching=$CACHING;

since there is a return statement just before it.

Monte
Back to top
View user's profile Send private message Visit poster's website
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Thu Apr 24, 2003 5:01 pm    Post subject: Reply with quote

duh! of course, I should have seen that. I was too excited about it. Nonetheless it appears that the original true cache value reverts when processing returns to the original template, since I still see a performance gain in the part of the page after this call. There is some extremely complex database querying and munging of results going on that slows the page down when caching is off.
Back to top
View user's profile Send private message Visit poster's website
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Thu Apr 24, 2003 6:50 pm    Post subject: Reply with quote

Well... I guess it's generally not a good idea to do this approach. {insert}s are _very_ slow. I had to deal with this issue quite often now and came to the conclusion that the best approach is to use a template that is not cached in which you include other files using include_php. there you can define wheter or not to cache a template. Of course you should keep any time-consuming things out of this index.tpl and move them to seperate, sub-included templates. I know well that this approach doesn't solve the problem of wanting non-cached templates within cached ones, but... well... I'm looking forward to this {nocache}-thing (if it's ever going to come), which will give us a great method of doing exactly what hinrichs wants to do...
Back to top
View user's profile Send private message
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Thu Apr 24, 2003 8:58 pm    Post subject: Reply with quote

Thanks for the advice! I am a little confused about your comment "there you can define whether or not to cache a template". Does this mean that it is possible to individually control caching of a page's included tpls somehow? Or are you just referring to the control of the top level tpl?

Really, the bottom line for us is that there is one sub-component of the page that is extremely slow. If the "top" level is not cached, that would be fine, if we could cache the slow sub-part.
Back to top
View user's profile Send private message Visit poster's website
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Fri Apr 25, 2003 12:39 am    Post subject: Reply with quote

i'll give an example:
index.php:
Code:
<?php
// ...
$smarty->caching = false;
$smarty->display("index.tpl");
// ...
?>

index.tpl:
Code:
....
{include_php file="foo.php"}
...
{include_php file="bar.php"}

foo.php:
Code:
<?php
// ...
$smarty->caching = true;
$smarty->display("foo.tpl");
$smarty->caching = false;
//...
?>

bar.php
Code:
<?php
// ...
$smarty->caching = false;
$smarty->display("bar.tpl");
// ...
?>
Back to top
View user's profile Send private message
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Fri Apr 25, 2003 4:14 pm    Post subject: Thanks! Reply with quote

Wow, thanks for your detailed response. I was afraid about turning caching on and off within the same smarty object.

Perhaps this method should be explained in the documentation under the caching section.

My curiosity is piqued, though--why would the insert be so much slower than the method described above? One advantage to using the insert I as I described is that it would eliminate the need for an extra php file that doesn't do much except turn off caching. Or, could one accomplish it by simply inserting php tags into the template that is not cached? Or doing a php_include within your template, of some php code that does not render a template itself, but just turns off caching in the middle of the template? This would also eliminate the need for the additional template file that stores the uncached info. I guess I could experiment here.
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


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

PostPosted: Fri Apr 25, 2003 11:09 pm    Post subject: Reply with quote

i think a compiler-plugin that emits the code for

Code:

$this->caching = true;
$this->display('cached.tpl');
$this->caching = false;"


into the template would also be handy. it saves the additional file-access of {include_php} and keeps the php out of the template in contrast to {php}...{/php}.

just a thought.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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