Passing variables between templates (Smarty)

For a couple of months now I’ve been working with the WCM system from Nstein.

A client uses it to deploy its website. It looks cool and easy to use but as I just said it looks so.

It uses Smarty templates to generate webpages and content. For this particular client WCM has been ‘pimped’ and a lot of stuff happened. First of all the use of templates is totally wasted, instead of having a top and footer templates you copy and paste the code anyway that’s not why I’m writing this post, the thing is that I had a tpl which was included inside another tpl nested in another tpl. When trying to set the value of one Smarty variable in the deepest nested tpl I couldn’t get it to work! Damn! a coworker helped me and after a couple of hours he gave me the answer.

Declare the variable on the highest level tpl, then assign it and finally call it through Smarty’s get_template_vars like this:

$variable_to_use = “some value”; //Declaration

$smarty_object->assign(“variable_assigned”,$variable_to_use); //Making it available for Smarty

$variable_to_use = get_template_vars(“variable_assigned”); //Call from the deepest nesting level tpl file

It’s really easy but I wasn’t aware of it.

I got so angry till my coworker gave me this tip that I thought it should be an experience to post in my blog so here it is.

Tags: ,

Comments are closed.