View previous topic :: View next topic |
Author |
Message |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Feb 08, 2010 5:57 pm Post subject: Smarty 3: variable variables |
|
|
In Smarty 2 if you had to access a dynamic variable name, something like this would be required:
Code: | {assign var="tmp" value="foo_$x"}
value is: {$foo.$tmp}
|
In Smarty 3, you can now handle this in the template syntax:
Code: | value is: {$foo_{$x}}
{if $foo_{$x} eq 0}...{/if} |
Last edited by mohrt on Tue Sep 03, 2013 2:11 pm; edited 1 time in total |
|
Back to top |
|
SiZE Smarty Rookie
Joined: 15 Sep 2010 Posts: 13
|
Posted: Mon Jan 24, 2011 7:25 am Post subject: |
|
|
I don't understund. Is it similar to php's
$var = 'hello';
$$var = 'world'
echo $hello; // print world
?
Cause I search working set for Smarty 2 and make something like this in my template:
php:
$this->assign( 'var_1', 'value1' );
...
$this->assign( 'var_N', 'valueN' );
tpl:
{foreach from=$vars item=v key=index}
{php}
$this->assign( 'new_var', $this->_tpl_vars[ 'var_'.$this->_tpl_vars['index'] ];
{/php}
{/foreach} |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Jan 24, 2011 1:32 pm Post subject: |
|
|
This should work:
Code: | {foreach from=$vars item=v key=index name="foo"}
{assign var="new_var" value="var_`$smarty.foreach.foo.index`"}
... do something with $new_var ...
{/foreach}
|
Although it would be easier to just assign your vars the right names from PHP, or assign an array of values instead of individual var names. |
|
Back to top |
|
pnoeric Smarty Rookie
Joined: 03 Mar 2006 Posts: 29
|
Posted: Wed Jun 22, 2011 10:03 pm Post subject: |
|
|
Monte, as a long-time Smarty fan, I just wanted to say I'm having a really great time looking at all the Smarty 3 changes and your implementation tips. Thanks again for a terrific bit of software... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Sep 26, 2011 1:46 pm Post subject: |
|
|
pnoeric wrote: | Monte, as a long-time Smarty fan, I just wanted to say I'm having a really great time looking at all the Smarty 3 changes and your implementation tips. Thanks again for a terrific bit of software... |
A big thanks goes to uwe and globe for Smarty 3, they put in a lot of time and effort on this project! |
|
Back to top |
|
CircleDev Smarty Rookie
Joined: 27 Jul 2011 Posts: 7
|
Posted: Fri Oct 21, 2011 3:02 am Post subject: |
|
|
mohrt wrote: | This should work:
Although it would be easier to just assign your vars the right names from PHP, or assign an array of values instead of individual var names. |
That is true but I found it simpler when using SmartyValidate to the assign like this
Code: | {validate id=$part.validate.id message=$part.validate.msg assign="error_{$part.validate.id}"} |
and for getting the value elsewhere
Code: | {"{$error_{$part.validate.id}}"} |
Admittedly the call to the var "looks" a bit messy but it allows for dynamic assigning of validation vars from php.
I use this style because many of the forms I am using have a variable amount of validation required
and i can then define from the business logic what gets validated and what doesn't.
I guess it depends on the context of the assignments
Comments appreciated |
|
Back to top |
|
martinello Smarty Rookie
Joined: 01 Oct 2012 Posts: 14
|
Posted: Fri Oct 05, 2012 6:41 am Post subject: Re: Smarty 3: variable variables |
|
|
Hello,
Is it valid for config variables also?
I tried {#foo_{$var}#}, it seems not working. mohrt wrote: | In Smarty 2 if you had to access a dynamic variable name, something like this would be required:
Code: | {assign var="tmp" value="foo_$x"}
value is: {$tmp}
|
In Smarty 3, you can now handle this in the template syntax:
Code: | value is: {$foo_{$x}}
{if $foo_{$x} eq 0}...{/if} |
|
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Oct 05, 2012 1:21 pm Post subject: |
|
|
It's currently not possible for config variables. I will put it on out todo list.
But the Smarty2 workaround can be used
Code: | {assign var="tmp" value="foo_$x"}
value is: {#$tmp#} |
|
|
Back to top |
|
MCMic Smarty n00b
Joined: 02 Sep 2013 Posts: 1
|
Posted: Tue Sep 03, 2013 7:31 am Post subject: Re: Smarty 3: variable variables |
|
|
Hello, for some reason I need to do this in smarty2.
But the code for smarty2 here is not doing the same thing as the one for smarty3:
Code: | {assign var="tmp" value="foo_$x"}
value is: {$tmp}
|
Assuming $x = 2, this will display "foo_2", the same as if you wrote foo_{$x}
Code: | value is: {$foo_{$x}} |
This will display the content of the var foo_2, which is what I want to do, but using smarty2. I tried several tricks without any success. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Sep 03, 2013 1:58 pm Post subject: |
|
|
For Smarty2 I have written a modifier a while ago.
You can find the varvar modifier within this topic http://www.smarty.net/forums/viewtopic.php?t=12678
Please note that the $smarty object needs to be global. |
|
Back to top |
|
MdeR Smarty n00b
Joined: 09 Oct 2013 Posts: 1
|
Posted: Wed Oct 09, 2013 12:22 pm Post subject: |
|
|
I use it like this sometimes:
Code: | {foreach $items as $item}
do something with {$somedata.{$item.id}}
{/foreach} |
|
|
Back to top |
|
|