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

passing variable from one template to another

 
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
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Sun Mar 29, 2009 5:05 pm    Post subject: passing variable from one template to another Reply with quote

Hi, I'm using smarty in x-cart. I've created a variable called $newsubtotal in a template called cart_subtotal.tpl.

I want to pass just the variable $newsubtotal to another template called cart.tpl.

I tried using {include file='modules/Fast_Lane_Checkout/cart_subtotal.tpl' newsubtotal1=$newsubtotal} but it included the whole template not just the variable $newsubtotal.

Any help would be greatly appreciated.

Thanks!
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Mar 29, 2009 5:28 pm    Post subject: Reply with quote

child templates inherit all the variables of the parent. If you pass a specific variable as a parameter, it is additive to the existing variables.
Back to top
View user's profile Send private message Visit poster's website
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Sun Mar 29, 2009 5:59 pm    Post subject: Reply with quote

Thanks for your quick reply. Unfortunately, I still don't quite understand..

if I assign a variable in the parent template,

{assign var="newsubtotal1" value="0.00"}

and a variable in the child template
called $newsubtotal = 250.


how do I call this variable back into the parent template? I assumed I can call it using the include function, but my format must be wrong because it includes the entire template {include file='modules/Fast_Lane_Checkout/cart_subtotal.tpl' newsubtotal1=$newsubtotal} ??
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Mar 29, 2009 6:14 pm    Post subject: Reply with quote

If you do this:

Code:
{assign var="newsubtotal1" value="0.00"}


That means that in the child template, you will have a local variable {$newsubtotal1} and it's value will be 0.00.

There isn't an easy way to pass variables back up the chain to parent templates. You can use a hack though. In the child template, do:

Code:
{capture name="mytotal"}9.99{/capture}


And in the parent you can retrieve with {$smarty.capture.mytotal}
Back to top
View user's profile Send private message Visit poster's website
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Sun Mar 29, 2009 6:58 pm    Post subject: Reply with quote

Thanks Monte! Worked like a champ. Why is it a "hack" ? Will it cause any issues in certain situations?

Thanks again
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Mar 29, 2009 9:48 pm    Post subject: Reply with quote

Its a hack in the sense that variables are not available to parent templates normally. But you can get them there with capture, even though capture wasn't meant for this. It won't break anything.
Back to top
View user's profile Send private message Visit poster's website
rshandel
Smarty Rookie


Joined: 29 Mar 2009
Posts: 16

PostPosted: Sun Mar 29, 2009 10:22 pm    Post subject: Reply with quote

Thanks again. it works great.
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