6

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

3
  • Can you add which version of smarty you're using? I'm pretty sure what you tried works on Smarty-3.1.2
    – Danack
    Dec 11, 2012 at 23:53
  • Is that a typo "I have a "links.tpl" ....using {include file="file.tpl"}" ?
    – Danack
    Dec 12, 2012 at 2:46
  • yes my mistake sorry that is links.tpl its ok on my file, version is also @version 2.6.2
    – Vladimir
    Dec 12, 2012 at 12:32

2 Answers 2

5

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.

3
  • thank you sir but this didn't work, neither of them, what i'm trying to do is to have a file.tpl which have all links as variables so i can changes all of design links from this tpl file as my script is zended, the second method does not suit this need :( thank you again sir
    – Vladimir
    Dec 11, 2012 at 18:37
  • thanks for reply i appreciate your help, well what i meant was this: File test/testInclude.tpl > {assign var='assignedVar' value='This is an included assignedVar'} and file test/test.tpl > {include file='test/testInclude.tpl'} assignedVar is {$assignedVar} and when i run test.tpl nothing the assigned var is empty, you did the opposite
    – Vladimir
    Dec 12, 2012 at 1:28
  • i think using variables of each page are limited only on that page, im not sure because script is zend and i can't read it, but its the only possibility, thank you so much for your help, wish you the best
    – Vladimir
    Dec 12, 2012 at 6:39
1

scope='global' works only in smarty 3, not in smarty 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.