RE: [SMARTY] Using smarty variables inside {php} {/php} blocks
| From: | Jaime Ashander | Date: | Wed, 04 Sep 2002 21:21:47 +0000 |
| Subject: | RE: [SMARTY] Using smarty variables inside {php} {/php} blocks | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-5931@lists.php.net to get a copy of this message | ||
> $this->_tpl_vars["bar"];
This works in {php} blocks, in the code below (1)
However if I include the code (2) in the calling script, then the
Variable $bar will be in the {php} scope as the assigned smarty variable
NO MATTER WHAT I HAVE SET THE VARIABLE TO IN THE CALLING SCRIPT
>>
>> Example:
>>
>> Foo.php:
>> ------------------
>> <?php
>> #my smarty is an innocent subclass that simply sets directories
>> require_once 'my_smarty.inc';
>>
>> $smarty= new My_Smarty();
>> $smarty->assign('bar', 'I want to be displayed');
$bar = 'I don't want to be displayed';
#(2)
>> $smarty->display('foo.tpl'); ?>
>>
>> Foo.tpl:
>> ------------------
>>
>> <html>
>> <body>
>> <!-- random code-->
>> Displayed via smarty:
>> {$bar}
>>
>> <br>
>>
>> Displayed via php:
>> {php}
echo $this->_tpl_vars['bar']; (1) this works
echo $bar; (3) because of (2) this also works
>> {/php}
>> </body>
>> </html>
>>
Output
------------------
Displayed via smarty: I want to be displayed
Displayed via php: I want to be displayedI want to be displayed
So to avoid the _tpl_vars business, just instantiate smarty variables in
the calling script
Through php varaibles with identical names. Then you can use these in
{php} blocks
If needed.
Cheers,
jaime