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

Smarty 3: {assign} overwrites $smarty->assign

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
Gibheer
Smarty n00b


Joined: 10 Sep 2009
Posts: 4

PostPosted: Tue Nov 17, 2009 4:21 pm    Post subject: Smarty 3: {assign} overwrites $smarty->assign Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 17, 2009 4:50 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 17, 2009 4:55 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Gibheer
Smarty n00b


Joined: 10 Sep 2009
Posts: 4

PostPosted: Wed Nov 18, 2009 7:43 am    Post subject: Reply with quote

thank you, that works for us.
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 -> Bugs 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