| View previous topic :: View next topic |
| Author |
Message |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7036 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: {$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 |
|
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: 7036 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: 7036 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: 13
|
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: 4173 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 |
|
|