Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

global variable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Zzuutt34
Smarty n00b


Joined: 31 Jul 2014
Posts: 1

PostPosted: Thu Jul 31, 2014 1:05 pm    Post subject: global variable Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Jul 31, 2014 4:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP