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

Smarty 3.1.16 capture append foreach problem

 
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
ShaQ
Smarty n00b


Joined: 27 Dec 2013
Posts: 1

PostPosted: Fri Dec 27, 2013 8:50 pm    Post subject: Smarty 3.1.16 capture append foreach problem Reply with quote

Hey Community,

i have the following code:

main.tpl:
Code:


 [{include file="test.tpl"}]

[{capture append="foo"}]
    bar
[{/capture}]

[{foreach $foo as $bar}]
    [{$bar}]
[{/foreach}]


test.tpl:
Code:

[{capture append="foo"}]
    foo
[{/capture}]


If i render the main.tpl, the output is: "bar".
But why? Confused

Friendly Greez,

ShaQ
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Sat Dec 28, 2013 6:24 am    Post subject: Re: Smarty 3.1.16 capture append foreach problem Reply with quote

ShaQ wrote:
Hey Community,

i have the following code:

main.tpl:
Code:


 [{include file="test.tpl"}]

[{capture append="foo"}]
    bar
[{/capture}]

[{foreach $foo as $bar}]
    [{$bar}]
[{/foreach}]


test.tpl:
Code:

[{capture append="foo"}]
    foo
[{/capture}]


If i render the main.tpl, the output is: "bar".
But why? Confused

Friendly Greez,

ShaQ


Why are you expecting the output to be different?

From the include manual page:

All assigned variable values are restored after the scope of the included template is left. This means you can use all variables from the including template inside the included template. But changes to variables inside the included template are not visible inside the including template after the {include} statement.

Is that the part you needed clarification on?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Dec 28, 2013 1:39 pm    Post subject: Reply with quote

You can pass the variables from the subtemplate to the calling template like this

test.tpl:
Code:
[{capture append="foo"}]
    foo
[{/capture}]
[{assign var='foo' value=$foo scope=parent}]
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