Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Smarty 3: variable variables

 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7238
Location: Lincoln Nebraska, USA

PostPosted: Mon Feb 08, 2010 5:57 pm    Post subject: Smarty 3: variable variables Reply with quote

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
View user's profile Send private message Visit poster's website
SiZE
Smarty Rookie


Joined: 15 Sep 2010
Posts: 13

PostPosted: Mon Jan 24, 2011 7:25 am    Post subject: Reply with quote

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


Joined: 16 Apr 2003
Posts: 7238
Location: Lincoln Nebraska, USA

PostPosted: Mon Jan 24, 2011 1:32 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
pnoeric
Smarty Rookie


Joined: 03 Mar 2006
Posts: 29

PostPosted: Wed Jun 22, 2011 10:03 pm    Post subject: Reply with quote

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


Joined: 16 Apr 2003
Posts: 7238
Location: Lincoln Nebraska, USA

PostPosted: Mon Sep 26, 2011 1:46 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
CircleDev
Smarty Rookie


Joined: 27 Jul 2011
Posts: 7

PostPosted: Fri Oct 21, 2011 3:02 am    Post subject: Reply with quote

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
View user's profile Send private message
martinello
Smarty Rookie


Joined: 01 Oct 2012
Posts: 14

PostPosted: Fri Oct 05, 2012 6:41 am    Post subject: Re: Smarty 3: variable variables Reply with quote

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


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

PostPosted: Fri Oct 05, 2012 1:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
MCMic
Smarty n00b


Joined: 02 Sep 2013
Posts: 1

PostPosted: Tue Sep 03, 2013 7:31 am    Post subject: Re: Smarty 3: variable variables Reply with quote

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


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

PostPosted: Tue Sep 03, 2013 1:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
MdeR
Smarty n00b


Joined: 09 Oct 2013
Posts: 1

PostPosted: Wed Oct 09, 2013 12:22 pm    Post subject: Reply with quote

I use it like this sometimes:
Code:
{foreach $items as $item}
  do something with {$somedata.{$item.id}}
{/foreach}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks 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