Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

isCached and template include

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
Perino
Smarty Rookie


Joined: 21 Jul 2010
Posts: 9

PostPosted: Wed Nov 30, 2011 11:01 am    Post subject: isCached and template include Reply with quote

Something seems off with isCached and template includes. I'm running the latest from trunk. My setup:

test.tpl:
Code:

{include file = "test2.tpl" cache_lifetime="10"}


test2.tpl:
Code:

{$a}


php:
Code:


$smarty = new Smarty;

[... dir setters ...]

if (!$smarty->isCached('test2.tpl')) {
  $smarty->assign('a', 'aaa');
}

$smarty->display('test.tpl');



The result is "unable to write file /wrt4ed60c0985fa7". Without the cache_lifetime in the template or the isCached check, everything is fine. I'm not sure if my implementation approach is wrong or if this is just a bug.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Nov 30, 2011 2:17 pm    Post subject: Reply with quote

This was a miss leading error which did occur because is_cached was called in the script without setting up caching.

Code:

$smarty = new Smarty;

[... dir setters ...]

// for isCached() calls of individual cache subtemplates you
// MUST set caching to CACHING_LIFETIME_SAVED
$smarty->caching = Smarty::CACHING_LIFETIME_SAVED;

if (!$smarty->isCached('test2.tpl')) {
  $smarty->assign('a', 'aaa');
}


// you may change the caching mode of the main template here
// this example when main template shall not be cached
$smarty->caching = Smarty::CACHING_OFF;

$smarty->display('test.tpl');


I will implement a meaningfull error message for the next release.
Back to top
View user's profile Send private message
Perino
Smarty Rookie


Joined: 21 Jul 2010
Posts: 9

PostPosted: Wed Nov 30, 2011 2:33 pm    Post subject: Reply with quote

Thanks, but it happens with Smarty::CACHING_OFF as well. So how would i go about caching only one included template but not the main template (say test2.tpl but not test.tpl). Setting caching to anything but OFF would require a cache lifetime of 0 seconds to exclude the main template from being cached (but this approach seems wrong and generates a huge overhead). Setting caching to OFF throws an error as stated above.

The documentation for {include} states:

cache_lifetime : Enable caching of this subtemplate with an individual cache lifetime

and

caching: Enable caching of this subtemplate

So shouldn't i be able to call isCached() on this sub template with CACHING_OFF as well?

Also, the caching of the sub template actually does work with caching set to CACHING_OFF.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Nov 30, 2011 3:09 pm    Post subject: Reply with quote

Yep. My example was for the case that you have used {include ... life_time = 10}.

There is no way that Smarty can know in the PHP script what caching mode you have used in the {include} tag.

So when you are using is_cached() on subtemplates you have to set up caching before is_cached().
Use $smarty->caching = Smarty::CACHING_LIFETIME_SAVED; with {include ... life_time = xxx};
Use $smarty->caching = Smarty::CACHING_LIFETIME_CURRENT; with {include .... caching}

And yes setting the caching mode in the script will only controll the behaviour of is_cache().
The subtemplate will always be cached as specified in the {include} tag. Otherwise you cound never cache a subtemplate with caching disabled for the main template.


If you don't want to have the main template cached, you have to turn caching off again after the is_cached() calls for the subtemplate as done in my example.

So the example will cache test2.tpl but not test.tpl
Back to top
View user's profile Send private message
Perino
Smarty Rookie


Joined: 21 Jul 2010
Posts: 9

PostPosted: Wed Nov 30, 2011 3:22 pm    Post subject: Reply with quote

I missed the part where you turned off the cache after the call. I fail to see why Smarty cannot know what caching mode i specify in the include tag (since it's also stored in the actual cache file). Obviously i'm not well read into the Smarty core, but i think this is a problem that can be fixed.

I will use your suggested approach for now though, thank you very much for your help!
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Nov 30, 2011 4:01 pm    Post subject: Reply with quote

Well there are other conditions to consider.

is_cached() is designed since Smarty2 to always return false if $smarty->caching is off for good reason.
Lets say you use caching and is_cache() for the main template.
Now you want for testing disable caching. If is_cache() would still return the status of an old cache file it would screw up the application as variables might not be assigend.
is_cached() does also not know the usage of a template. I could be used as subtemplate or as main template.

There is no save automated way to get all cases covered and we can run in all sorts of side effects.

Beleave me we spend already hours of thinking about this.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Nov 30, 2011 4:35 pm    Post subject: Reply with quote

The fix for the missguiding exception is in the SVN trunk now and will also be included in the next 3.1.6 release
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 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