Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a tpl file with variables (Smarty)

Tags:

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 scope=global to variable and parent to include but nothing happened

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

like image 257
Vladimir Avatar asked Oct 06 '22 17:10

Vladimir


1 Answers

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}

<br/>

File test/test.tpl

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

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

<br/>

Outputs

   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.

like image 94
Danack Avatar answered Oct 10 '22 02:10

Danack