Vladimir Vladimir - 3 years ago 374
Smarty Question

Include a tpl file with variables (Smarty)

I have a "links.tpl" file which contains lines with many variables such as below

{assign var=link_main value="index.php"}
{assign var=link_login value="?a=login"}

but when i include this file in home.tpl using
{include file="file.tpl"}

the variables
{$link_main}, {$link_login}
are not included

i put the
{assign var=link_main value="index.php"}
in home.tpl and it works but not from included file

i have tried adding
to variable and parent to include but nothing happened

I tried the last few hours finding a solution, any help is appreciated

Answer Source

What you're doing should work. Testing on my Mac:

File test/testInclude.tpl

{assign var='assignedVar' value='foo' scope='global'}

assignedVar in child is {$assignedVar}


File test/test.tpl

{include file='test/testInclude.tpl'}

{if isset($assignedVar)}
    assignedVar is set in parent {$assignedVar}
    assignedVar is not set in parent



   assignedVar in child is foo 
   assignedVar is set in parent foo 

That really should work - if not then there's something really weird going on with your instance of Smarty. Could it be that the template is being cached and not being regenerated when the variables are changed?

Obviously you should figure out what's going wrong, but if all else fails you could also use the alternative capture function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download