|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
maka Smarty Rookie
Joined: 15 May 2003 Posts: 21 Location: Maringá-PR BRASIL
|
Posted: Wed Oct 01, 2003 6:53 pm Post subject: template var to parent template |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Oct 01, 2003 9:00 pm Post subject: |
|
|
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 |
|
maka Smarty Rookie
Joined: 15 May 2003 Posts: 21 Location: Maringá-PR BRASIL
|
Posted: Wed Oct 01, 2003 9:19 pm Post subject: |
|
|
Thank's, I didn't know that!
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Oct 01, 2003 9:46 pm Post subject: |
|
|
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 |
|
maka Smarty Rookie
Joined: 15 May 2003 Posts: 21 Location: Maringá-PR BRASIL
|
Posted: Wed Oct 01, 2003 10:48 pm Post subject: |
|
|
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 |
|
|
|
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
|