Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Scope problem with included templates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
bobokiki
Smarty n00b


Joined: 08 Aug 2003
Posts: 2

PostPosted: Fri Aug 08, 2003 11:52 pm    Post subject: Scope problem with included templates Reply with quote

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. Smile
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Sat Aug 09, 2003 12:26 am    Post subject: Reply with quote

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
View user's profile Send private message
bobokiki
Smarty n00b


Joined: 08 Aug 2003
Posts: 2

PostPosted: Sat Aug 09, 2003 4:18 am    Post subject: Reply with quote

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... Crying or Very sad

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP