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

template var to parent template

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


Joined: 15 May 2003
Posts: 21
Location: Maringá-PR BRASIL

PostPosted: Wed Oct 01, 2003 6:53 pm    Post subject: template var to parent template Reply with quote

Hi,
Supose I have a traditional situation of a template file main.tpl that include
header.tpl, body.tpl and footer.tpl

The title html tag is defined in header.tpl, but I want to change it to a value comming from inside body.tpl.

in header.tpl I did: <title>{$title|default:"untitled"}</title>

and in body.tpl I did: {*title=Main Page*} for example.

To complete I create a pre filter that pregmatch the comment from body.tpl and assign it to $smarty object that is passed by reference as the second parameter at my filter. Every thing works fine, excepty for the problem that seens that smarty call each template with a diferent $smarty object, not with the same. The vars that I assign exist just for the current tpl.
Is it a bug or a feature??
I think it's a bug, since if I define a var in the main.php ou inside each template, the var is visible in all the next template.

there's a way to do this or something similar?
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Oct 01, 2003 9:00 pm    Post subject: Reply with quote

a prefilter is called at compile-time, not at display-time. the $smarty-object passed by reference is actually an instance of Smarty_Compiler and not the instance of (your subclass of)Smarty. the Smarty_Compiler-object is a different one for each template and the prefilter is called only at the first time of display() of your template so it is not a good place to assign variables.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
maka
Smarty Rookie


Joined: 15 May 2003
Posts: 21
Location: Maringá-PR BRASIL

PostPosted: Wed Oct 01, 2003 9:19 pm    Post subject: Reply with quote

Thank's, I didn't know that! Wink

just two question:

What interesting thing could I do whith this $smarty inside a filter?

there's a way to do this or something similar?
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Oct 01, 2003 9:46 pm    Post subject: Reply with quote

you can call all methods of $smarty because Smarty_Compiler is a subclass of Smarty. i don't know if that is "a interesting thing".

to get access to the "real" $smarty we can dirty our hands a little:

before you call display() which triggers compilation:
Code:
$smarty->assign_by_ref('smarty', $smarty); /* add a reference to smarty. this reference is invisible inside a template, because of the fact that {$smarty} is handled in a special way there */


in your prefilter:
Code:
function prefilter($code, &$compiler) {
$smarty =& $compiler->get_template_vars('smarty');
/* $smarty is the object whose display()-method was called now */


but again: assign() doesn't help you much. the second time a template is displayed it isn't compiled, so no prefilter is called and no variables are assigned.

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
maka
Smarty Rookie


Joined: 15 May 2003
Posts: 21
Location: Maringá-PR BRASIL

PostPosted: Wed Oct 01, 2003 10:48 pm    Post subject: Reply with quote

nice trick, I test it here and works only while the cache has not expired.
The solution would be insert code in the compiled file.

Would it be possible if I use a pre_filter with post_filter too? or perhaps an output_filter?

will try this tonight
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