Re: [SMARTY] assign in included files
| From: | Monte Ohrt | Date: | Fri, 17 May 2002 18:51:07 +0000 |
| Subject: | Re: [SMARTY] assign in included files | ||
| References: | 1 2 3 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-4281@lists.php.net to get a copy of this message | ||
OK I digress, some of these issues were already changed some time ago, sorry about that :-)
So basically we are at the issue of extraction for imbedded variables in quotes. IMHO variable extraction is an overhead, one that could be avoided by adjusting the syntax of imbedded vars instead.
Andrei Zmievski wrote:
On Fri, 17 May 2002, Monte Ohrt wrote:-- Monte Ohrt <monte@ispi.net> Director of Technology, ispi Inc.This is a problem that has a bit to do with how Smarty handles assigned variables. I'd like to start a discussion on this issue, it is something I've discussed with Andrei before. When you assign a template variable, it gets added to an array in the Smarty class called $_tpl_vars; this keeps track of the assigned variables. When you do this in a template: {$foo} You would _think_ that Smarty does this: <?php echo $this->_tpl_vars['foo']; ?> but it doesn't. Instead, this happens: <?php echo $foo; ?>Wrong, check the code. It does exactly the first thing you mention.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. 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.It has nothing to do with extract(). The call to extract() is there for one single reason: variable interpolation inside quoted attribute values, e.g. {include file="$template/index.tpl"}.Now the ultimate question: why does Smarty extract the variables? Why not just echo $this->_tmp.var ?See above.This reason is for the variables imbedded within quotes like so: {include file="$foo/index.tpl"} Here we imbed $foo in quotes, and the variable must be in the local scope for it to work. 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. For a half-baked fix, we could simply change {$foo} to this: <?php echo $this->_tpl_vars['foo']; ?>It already does this.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.The reason that variables assigned in a subtemplate are not accessible to the parent one is because subtemplate has its own scope. The $_tpl_vars are saved before smarty_include() is called and then restored. It's not a bug, it's a design issue.-Andrei http://www.gravitonic.com/Programming X-Windows is like trying to find the square root of pi using roman numerals. - Henry Spencer