|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
bobokiki Smarty n00b
Joined: 08 Aug 2003 Posts: 2
|
Posted: Fri Aug 08, 2003 11:52 pm Post subject: Scope problem with included templates |
|
|
Hi, I've been playing with Smarty for a few days now.
I can't seem to figure out how to have an included template assign a global var:
MAIN.TPL
{include file="foo.tpl"}
{$bar}
FOO.TPL
{assign var="bar" value"35"}
MAIN.TPL doesn't see the $bar variable, presumably because it's local to FOO.TPL . I assume I'd have to assign "bar" as a global somehow -- not sure how that's done?
Thanks for the help. |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Sat Aug 09, 2003 12:26 am Post subject: |
|
|
Hi,
"globals" must to be assigned with $smarty->assign('bar', 35), or if the value is suitable for a config.file, put it in there and a {config_load} in your main template.
{assign}ments and {configs_load}s you do in "sub templates" are local to them.
With assign() in PHP, or {config_load} in the main template, you can then use all the variables and configs in the sub-templates.
The following is a blatant assumption as I haven't tried it (yet), but if 'foo.tpl' MUST assign a value to $bar, you may try to "prepare" Smarty for it using[php:1:626eed34bc]<?php
$dummyfoo = 0; // just to have sth. to reference
$smarty->assign_by_ref('bar', $dummyfoo);
?>[/php:1:626eed34bc]
I dunno if this works, but because $smarty already knew about 'bar' in the beginning it should work.
Have fun,
CirTap |
|
Back to top |
|
bobokiki Smarty n00b
Joined: 08 Aug 2003 Posts: 2
|
Posted: Sat Aug 09, 2003 4:18 am Post subject: |
|
|
Thanks for the response.
I was hoping to this via templates only. I guess I can just copy the contents of the included file into MAIN.TPL. The include file is only called from a couple other templates so I'll do the same. I just hate having redundant info all over the place...
CirTap wrote: | Hi,
"globals" must to be assigned with $smarty->assign('bar', 35), or if the value is suitable for a config.file, put it in there and a {config_load} in your main template.
{assign}ments and {configs_load}s you do in "sub templates" are local to them.
With assign() in PHP, or {config_load} in the main template, you can then use all the variables and configs in the sub-templates.
The following is a blatant assumption as I haven't tried it (yet), but if 'foo.tpl' MUST assign a value to $bar, you may try to "prepare" Smarty for it using[php:1:f321e01dc4]<?php
$dummyfoo = 0; // just to have sth. to reference
$smarty->assign_by_ref('bar', $dummyfoo);
?>[/php:1:f321e01dc4]
I dunno if this works, but because $smarty already knew about 'bar' in the beginning it should work.
Have fun,
CirTap |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|