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

Variable scope

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


Joined: 08 Dec 2009
Posts: 3

PostPosted: Tue Dec 08, 2009 2:23 pm    Post subject: Variable scope Reply with quote

I seem to have a problem with nested templates and their variable scope. Nesting templates that are using the same variable names as their parent seems not possible as they are just overwriting their parent's variables, and there is no way of controlling variable scope downwards, e.g. a 'private' or 'template' scope.

What I'm trying
Code:
mytemplate.tpl:
{foreach $items as $item}
    {myfunction id=$item.id}
{/foreach}


where myfunction is doing e.g.

Code:
$data = new Smarty_Data(array('items'=>$items));
$smarty->display('mytemplate',$data);


I was trying to do this without automatic access to parent variables, unless explicitly via {$parent.<varname>} or so.

Correct me if I'm wrong or missing something basic here..

---
First post btw, so hello to you all!
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Dec 08, 2009 4:12 pm    Post subject: Reply with quote

That is not true. In Smarty 3 variables have a scope.

Subtemplates included with the {include} can read all variables which are known in the calling (parent) template. There is just on exception that the special smarty variables $smarty.foreach... and $smarty.section.... are only know within the template where the loops are defined.

If a variable gets assign (written) in a subtemplate its value has by default local scope. So if you read in a subtemplate a variable which is defined in the calling template and modify it, the new value is only known in the subtemplate. The calling template still keeps its original value.

You can overwrite the default scope when you assign a value in the subtemplate. For example:
{assign var=foo value=0 scope=parent} or {$foo=0 scope=parent}
In this case the new value of $foo can also be seen in the calling (parent) template.


I currently do not understand your example:

There are you saying that the template is calling the plugin function myfunction which itself is calling the display again. This seems to be recursive calls which do not make any sense to me. What are you trying to do?
Back to top
View user's profile Send private message
hanzz
Smarty n00b


Joined: 08 Dec 2009
Posts: 3

PostPosted: Tue Dec 08, 2009 6:27 pm    Post subject: Reply with quote

It's indeed about recursive calls to the same template. After lots of testing I was convinced it was the foreach variable that got modified (I was getting timeouts on the templateparser).

But it's not, just an unlucky coincidence in my php + debugger + test, hiding a mistake in my own code and keeping me busy.. Embarassed

Tnx anyway Smile
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 -> 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