Re: [SMARTY] assign in included files

From: Date: Fri, 17 May 2002 18:41:06 +0000
Subject: Re: [SMARTY] assign in included files
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4279@lists.php.net to get a copy of this message
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; ?> 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. Now the ultimate question: why does Smarty extract the variables? Why not just echo $this->_tmp.var ? 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']; ?> 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? Monte Pupeno wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 If I assign a variable in a file and then include that file, shouldn't that variable be available in tha file that is including it ? Thank you. - -- Pupeno: pupeno@pupeno.com.ar http://www.pupeno.com.ar -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE85TlFWhjnmGRO4vQRAi1WAJ4k6JVaZ5fRQQM9wwYrMe5OGEnzqACgiiit FEMYgulEm4xWU9opEGQ/Gi0= =oLM5 -----END PGP SIGNATURE-----
-- Monte Ohrt <monte@ispi.net> Director of Technology, ispi Inc.

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