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

{nocache} in sub-template

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


Joined: 14 Oct 2009
Posts: 7
Location: Switzerland

PostPosted: Wed Oct 14, 2009 2:43 pm    Post subject: {nocache} in sub-template Reply with quote

hello smarty community,

i need help with {nocache} from smarty3. it works fine until i use it in a
sub-template wich i {include} in my framework.tpl. let me explain and you'll see the error maybe.



init.php
[[more smarty init+conf, etc here]]
$smarty->caching = 2;
$cache_group = "template_name|page|param1|param2"; // just for demonstration how i name this var.
$compile_id = $cache_group;
$smarty->assign('page', 'foo');
$smarty->display('framework.tpl', $cache_group, $compile_id);



framework.tpl
<html>
<head>
<title>test {$page}</title>
</head>
<body>
{if $page}
{include file="{$page}.tpl" page="{$page}"}
{/if}
{$smarty.now|date_format:"%D %H:%M:%S" nocache} <!-- NOCACHE WORKS HERE -->
</body>
</html>



foo.tpl
<p>
<!-- NOCACHE DOESN'T WORK HERE -->
nocache test 1: {nocache} {$smarty.now} {/nocache}<br />
nocache test 2: {$smarty.now nocache}
</p>



so...again. {nocache} works fine until i use it in a {included} template. further i'm asking me if it is okay to set the same compile_id as cache_group.

anyone an idea how i can fix this? i don't want to {insert} the whole foo.tpl because i just need some little parts dynamic.


thanks in advance for reading/thinking Smile
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Oct 14, 2009 3:30 pm    Post subject: Reply with quote

Here we have a special case which is not (yet) covered automatically by Smarty.

With a constant file name of the included template it would work as Smarty3 is merging the compiled template together at compiletime for speed optimizations. In this case everything would be fine also with nocache sections in the included template.

The above can't be done if the file name of the included template is variable. However the cache does include the output of your subtemplate at the time the page was first called. Smarty3 does not know that the output of the subtemplate did contain nocache code.

The current workaround is to inlcude the subtemplate nocache when it does include nocache code.

{include file="{$page}.tpl" page=$page nocache}

NOTE: you don't need double quotes at the page parameter...

I think you don't need a compile id at all. It is only needed if you have different versions of the source template with same name.
Back to top
View user's profile Send private message
arT2
Smarty Rookie


Joined: 14 Oct 2009
Posts: 7
Location: Switzerland

PostPosted: Wed Oct 14, 2009 3:43 pm    Post subject: Reply with quote

Quote:
The current workaround is to inlcude the subtemplate nocache when it does include nocache code.

{include file="{$page}.tpl" page=$page nocache}

thanks a lot, that works!

Quote:
I think you don't need a compile id at all. It is only needed if you have different versions of the source template with same name.

well i have different templates. for example /templates/blueish/ and /templates/greenish/. that's why i added a cache_group and compile_id. correct?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Oct 14, 2009 4:10 pm    Post subject: Reply with quote

To keep the different templates separated you need a compile_id. For example compile_id = 'blueish' or compile_id = 'greenish'.

The cache_id is used if you have different content for the same template version. For example if you load different content from a part database. In this case compile_id = $part_id.

Both are independent from each other. Depending on the application you may need none, one of these or both.

compile_id should be just a simple id for the template version. Remember each different compile_id will cause smarty to recompile the template and create a new version of the compiled template. It should never be content dependend as you will fill up your disc with identical compiled templates and have bad performance.

The cache_id part doen not need to contain the template version because this is redundant (covered already by the compile_id).
Back to top
View user's profile Send private message
arT2
Smarty Rookie


Joined: 14 Oct 2009
Posts: 7
Location: Switzerland

PostPosted: Wed Oct 14, 2009 4:46 pm    Post subject: Reply with quote

Okay this is now much clearer for me.

So i've changed it to:

$cache_group = 'pagename|param1|param2'
$compile_id = 'template_name'


Thanks again!
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Oct 14, 2009 5:00 pm    Post subject: Reply with quote

In your case the template_name for the compile_id must just be the name of the folder 'bueish', 'greenish' etc. It does not need to include the template (file) name itself.
Back to top
View user's profile Send private message
arT2
Smarty Rookie


Joined: 14 Oct 2009
Posts: 7
Location: Switzerland

PostPosted: Wed Oct 14, 2009 5:08 pm    Post subject: Reply with quote

Jup that's what i've made. Made a mistake by writing 'template_name' in my previous post. I meant 'template_dir_name' Rolling Eyes
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