RE: [SMARTY] Using smarty variables inside {php} {/php} blocks
| From: | Ferdinand Beyer | Date: | Thu, 05 Sep 2002 08:06:44 +0000 |
| Subject: | RE: [SMARTY] Using smarty variables inside {php} {/php} blocks | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5932@lists.php.net to get a copy of this message | ||
On 4 Sep 2002 at 14:21, Jaime Ashander wrote:
>
>
> > $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
No, that is not true. Let me explain the PHP scopes used in Smarty:
The variables set in your calling script are in the global scope. The
variables you assign() to Smarty are in the 'Smarty scope' (in the
$_tpl_vars array). The {php} _AND_ {include_php} tags are called
from _INSIDE_ the Smarty methods, so they are _INSIDE_ the
scope of a Smarty method!
This is why you can access $this in {php} and {include_php} tags.
But Smarty is clever (smart) - it uses extract() from time to time to
extract the Smarty scope to the method scope - see your compiled
template, you will often find something like this:
if ($this->_extract) extract($this->_tpl_vars);
That's why you can do something like that:
$smarty->assign('world', 'whole world');
{tag attr="Hello $world"}
(should work, not tested)
So you can sometimes access template vars directly inside {php} and
{include_php} tags - this is number 3 in your example. By the way:
Did you notice that if no. 3 worked because of no. 2, it would have
output "I DON'T want to be displayed"??? So it can't come from no. 2
:-)
Finally, this is the CLEANEST way:
{assign var="x" value="Hello World!"}
{php}
extract($this->_tpl_vars);
echo $x;
{/php}
Outputs: "Hello World!";
If you want to use template vars inside {php} or {include_php} tags,
use extract($this->_tpl_vars) to extract the vars from the Smarty
scope to the method scope!
--
Ferdinand Beyer
<fbeyer@clickhand.de>