|
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 |
Gibheer Smarty n00b
Joined: 10 Sep 2009 Posts: 4
|
Posted: Tue Nov 17, 2009 4:21 pm Post subject: Smarty 3: {assign} overwrites $smarty->assign |
|
|
We have a little problem here with the assign.
We have a $smarty object, which we give a little function, which should set 'foobar' to the variable.
Code: | $smarty->register_function('little_func', 'little_func');
function little_func($params, $smart) {
$smart->assign($params['assign'], 'foobar');
}
|
In the Template we have the following code.
Code: | {assign var="foo" value="bar"}
value: {$foo}<br />
{little_func assign="foo"}
value: {$foo}
|
The expected output would be
Code: | value: bar<br />
value: foobar |
but we get
Code: | value: bar<br />
value: bar |
I don't think, that this is normal, espacially, we did not find any hint in the documentation, that assign overrules the $smarty->assign. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Nov 17, 2009 4:50 pm Post subject: |
|
|
This is a situation that is not BC with Smarty 2, and has not been implicitly documented yet.
With Smarty 3, the current template is it's own object, so assigning the var to $smarty (the global object) will not see the change locally. What you need to do is assign the var to the local template object, which is the 3rd param in Smarty 3:
Code: | function little_func($params, $smarty, $tpl) {
$tpl->assign($params['assign'], 'foobar');
} |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Nov 17, 2009 4:55 pm Post subject: |
|
|
Well that wasn't completely correct. This situation only happens if you are assigning an already-assigned var. If you assign a (new) global var, the local tpl should pick it up. The search order is local, if not found, then next up the chain, etc. until global is reached. |
|
Back to top |
|
Gibheer Smarty n00b
Joined: 10 Sep 2009 Posts: 4
|
Posted: Wed Nov 18, 2009 7:43 am Post subject: |
|
|
thank you, that works for us. |
|
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
|