assigned variable scope
| From: | Monte Ohrt | Date: | Thu, 23 May 2002 21:56:08 +0000 |
| Subject: | assigned variable scope | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-103@lists.php.net to get a copy of this message | ||
I'd like to get the variable scope issues out on the table :-) Some of this stuff has been discussed before, but I'll try to bring it all together in one cohesive discussion.
assigned variable scope
-----------------------
Currently, if you do this in a template:
{include file="foo.tpl"}
Smarty generates source code like this:
<?php $_smarty_tpl_vars = $this->_tpl_vars;
$this->_smarty_include("foo.tpl", array());
$this->_tpl_vars = $_smarty_tpl_vars;
unset($_smarty_tpl_vars);
?>
Notice what it does: it creates a copy of the currently assigned template variables, includes the file, then reverts back to the saved variable state. That means that any variable assigments changed within the included template will be lost. (NOTE: If you call clear_all_assign() in here, they'll all come back!)
I know one reason why local scoping was done, if you pass variables to the include like so:
{include file="foo.tpl" title="blah"}
Then {$title} is only available within the included file scope.
I tested this with Smarty 2.0 and the assigned vars _were_ available after the include... mistake or intentional change?
Another mental note is the resources taken by copying the entire variable array on each call to {include}.
config_load variable scope
--------------------------
Now concerning config variable scope. Currently, config loaded variables are lost after coming out of an included file too, but they are _also_ lost between calls to fetch() and display(), unlike assigned variables which persist.
So I want to ask what your thoughts are, what is a good sensible behavior? How would I, for instance, assign template variables available at a global scope, or how _should_ this be handled?
Maybe all assigned variables should be global scope, with the exception of vars passed to {include}. As for config vars, they could all be global scope too, but provide a way to clear them.
Thoughts?
Monte