Re: [SMARTY] Variable bleed??
| From: | Gerard Samuel | Date: | Tue, 10 Dec 2002 22:50:15 +0000 |
| Subject: | Re: [SMARTY] Variable bleed?? | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-6721@lists.php.net to get a copy of this message | ||
Ok, just got back from debugging.
This is what Ive found out as an example ->
PHP File
<?php
$foo = array('this', 'that');
$smarty->assign('data', $foo);
$smarty->display('index.tpl');
$smarty->clear_assign('data');
?>
Template File
<html>
{foreach item=$var from=$data}
{$var}
{/foreach}
</html>
When smarty is executing the template, it has 2 variables, the variable $data, and the newly created $var.
In my situation, $var is the problem. To get around my problem, I added it to the list in the clear_assign() method ->
$smarty->clear_assign(array('data', 'var'));
Now is this supposed to be the expected behaviour?? I don't know. Is it possible to have {foreach} clean up after it self?
If not, maybe add to the documentation on foreach and maybe section, that newly created variables by the loop,
needs to cleared to avoid clashing.
Ok, back to work for me. Got to find all those {foreach} loops...
Thanks for listening.....
Monte Ohrt wrote:
template variables are currently extracted into PHP name space, then the template files get included. This may change though, there has been threads on in before. On Tue, 2002-12-10 at 15:30, Gerard Samuel wrote:-- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/I just ran into a scenario, and Im not sure how its happening. I have a page that displays "blocks" along with center content. One block on the left is a poll. In the poll block's php code Im using an array $data. One template variable comes from the php $data array -> $smarty->assign('poll_option', $data['options']); In all my scripts I make it a habit to assign, display, then clear template variables. Now on this one page, the center content is getting a template array "data". $smarty->assign('data', $file); Im using {foreach} to loop throught the template array like so -> {foreach item=line from=$data} {$line} {/foreach} For some reason, the template loop is displaying the last row of the array $data from the poll block. The quick and easy could be to rename the center content template variable. But what is the general consensus about variable naming for templates? Is it possible for php variables, and template variables to clash? What is additionally confusing is that both the poll block and center content are in functions. Those variables should only exist in their particular functions. Ill keep looking, but if anyone has run into something like this, Ill be glad to hear what you have to say... Thanks for listening... -- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/