Re: [SMARTY] assign in included files

From: Date: Fri, 17 May 2002 19:45:53 +0000
Subject: Re: [SMARTY] assign in included files
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4288@lists.php.net to get a copy of this message
"Monte Ohrt" <monte@ispi.net> schrieb: > This means that the variable $foo must be available in the local scope. > To accomplish this, Smarty must extract() the assigned variables before > calling the template. So any time you call display() or fetch(), smarty > does an extract($this->_tpl_vars) first. That means that any time you > call {assign ...} in your template, smarty will assign the variable, > then _immediately_ re-extract the assigned variables again (sound like a > hack?) So now to your problem: the assigned variable is not there when > going back to a parent template. Because it wasn't assigned to the parent... So if i want a variable to be in parent scope, i have to assign it to the parent and not to the child. Otherwhise i don't have to wonder, why it's not available in the parent template. Question: An assigned variable for an included template must have a source... in the including template, right? So, why not use this source in the parent? And instead of doing things like this: {include file="template.tpl" myVar=$someVar} just use something like this: {assign var=myVar value=$someVar} {include file="template.tpl"} and there wont be any problem. > The problem is, when you come back out > of your included template, the scope of the extraction is back to what > it was before you included it. I don't think, this is a problem. To me this seems like a feature and a must for a clean handling. I think this kind of handling is absolutely logical and one of the big points in Smarty. For example i use to do things like this: {section name=block loop=$content.blocks} {include file=$content.blocks[block].template content=$content.blocks[block].content} {/section} and i just love the abtility to do this. But this, for example, would not work with auto-assign to parent scope. [snipped] > Can we fix it? Ideally, the variables should never be extracted, they > should always come straight from $this->_tpl_vars and not have the > performance overhead of extraction (actually kind of expensive!) If we > wanted to do this, Smarty must parse every quoted string for variables > and adjusting the syntax. Andrei can elaborate on the issues this brings > up, but I would hope it is possible. Hope, the parsing is only ment to be done while compiling... :-)) > For a half-baked fix, we could simply change {$foo} to this: > > <?php echo $this->_tpl_vars['foo']; ?> One step forward maybe better than no step of two steps... if it doesn't cause problems. > This fixes part of the issue, but doesn't fix the problem with imbedded > variables... and I'm not sure if it would break anything else. > > Thoughts? The outer logical (scope-)handling of assigned vars within the templates should not be changed in any case. It's just fact, that there's no good reason to do so. Just my two cents... To avoid extracting each time, assigned variables could be added to a sub-key within $this->_tpl_vars, using an identifier for the include. Something like $this->_tpl_vars[$id]['foo'], while $id could be a simple counter of includes and $id[0] would be the topmost template. So the local scope of a template can be identified by $id. To save memory the subkey could just be unset() after gettings it's contained variables. Thoughts or comments? Greetings, Norbert

« previous php.smarty.general (#4288) next »