|
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 |
Girish Smarty Rookie
Joined: 05 Jan 2017 Posts: 5
|
Posted: Fri Jan 13, 2017 11:45 am Post subject: Global variable scope issue |
|
|
Code: | {function name=testing data=''}
{assign var=var_a value=$data scope="global"}
{/function}
{testing data=1}
1. {$var_a}
{testing data=2}
2. {$var_a} |
Expected Output:
1. 1
2. 2
Actual Output:
1. 1
2. 1
What is wrong with this? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sat Jan 14, 2017 12:14 am Post subject: |
|
|
I think the use of scope is not applicable here. Scope wants to have the variable and value be seen in the template hierarchy (that is, included sub-templates), not the logical hierarchy (that is, functions).
So, we should accept that variables created or modified inside a Smarty function will not be known outside that function.
Also, unlike a PHP function where arguments are input to get an output that can be used elsewhere, a Smarty function does not output anything other than the actual textual rendering of its task.
So, try:
Code: |
{function testing} {* short-form *}
{$data}
{/function}
1. {testing data=1}
2. {testing data=2}
|
|
|
Back to top |
|
Girish Smarty Rookie
Joined: 05 Jan 2017 Posts: 5
|
Posted: Mon Jan 16, 2017 8:22 am Post subject: |
|
|
You mean variable scope is only limited with template hierarchy?
We want to use those variable at multiple places so if we go with this approach we have to call respective function at many times.
Can't we just return an array from function and that array I can use to array to display respective values or do we have alternative for same? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Jan 17, 2017 1:42 pm Post subject: |
|
|
Just assign a value to the template object.
Don't invent wheels. |
|
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
|