|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Zzuutt34 Smarty n00b
Joined: 31 Jul 2014 Posts: 1
|
Posted: Thu Jul 31, 2014 1:05 pm Post subject: global variable |
|
|
Hello
I have a template with this function
Code: |
{function searchpresent}
{assign var="id_present" value="false" scope="global"}
{foreach $parents_category_id as $all_parent}
{if $parent == $all_parent}
{assign var="id_present" value="true" scope="global"}
{/if}
<span>valeur: {$all_parent} test: {$id_present}</span>
{/foreach}
{/function}
{searchpresent parent=$parent}
<span>result: {$id_present}</span>
{if $id_present == "true"}
.....
{/if}
|
display output
Code: |
valeur: 5 test:false
valeur: 3 test: true
result: false
|
the variable $id_present is not transmit, why ?
thank |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jul 31, 2014 4:35 pm Post subject: |
|
|
See http://www.smarty.net/docs/en/language.function.function.tpl
Quote: | You can use all variables from the calling template inside the template function. Changes to variables or new created variables inside the template function have local scope and are not visible inside the calling template after the template function is executed. |
This is a restriction when using template function. The scope parameter in {assign} does not help.
The workaround is {capture}
Code: | {capture name='id_present'}true{/capture} |
and
Code: | {if $smarty.capture.id_present == "true"} |
|
|
Back to top |
|
|
|
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
|