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

Variable variables anyone?

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
mbouclas
Smarty Rookie


Joined: 10 Feb 2004
Posts: 6

PostPosted: Mon Dec 27, 2004 1:40 pm    Post subject: Variable variables anyone? Reply with quote

I’m trying to use variable variables in the smarty templates but I’m not sure about the syntax. The result I’d like to accomplish should be something like this in PHP:
"string_".$var1 or the more complex scenario of $var1."_".$var2
Where var1 it’s usually a counter or an element of an array.
The main idea would be to have a {$variable_variable->func('property')}
where variable_variable the solution to my problem.
Obviously I can’t do this: {$"string_"$var1} and I run out of ideas.
Is there a modifier plugin to do the trick or have i missed something in the syntax?
Any suggestions ?
Back to top
View user's profile Send private message Send e-mail
gclift
Smarty n00b


Joined: 16 May 2004
Posts: 2

PostPosted: Thu Dec 30, 2004 9:16 pm    Post subject: back ticks Reply with quote

Back ticks and assign should do what you are trying.

{ assign var="myVar" value="`$var1`_`$var2`" }
{ $myArray.$myVar }


REF:
http://smarty.php.net/manual/en/language.syntax.quotes.php


I hope this helps!
Gary
Back to top
View user's profile Send private message
mbouclas
Smarty Rookie


Joined: 10 Feb 2004
Posts: 6

PostPosted: Thu Dec 30, 2004 11:46 pm    Post subject: Reply with quote

Getting closer, here's a better example of the problem i'm dealing with. In PHP:
for ($i=0;3 > $i ;$i++)
{
${"a_".$i} = new class_name;
$smarty->assign_by_ref("a_".$i,${"a_".$i});//assigned template variable
}//END OF FOR

so now we have passed three variables to smarty named $a_1,$a_2 and $a_3 holding the object i need to use.

The practical problem i'm having is how to recreate these variable names in smarty. Suppose i have a section with the exact number of iterations, how do you create something like this {$a_1->func_name()} where a should be a string and 1 to 3 the $smarty.section.section_name.loop value. The problem is that i can't get the syntax to produce a variable name in the right format.

Not sure i'm making my self perfectly clear but i hope you got an idea of what i have in mind.
Any help will be greatly appreciated.
Back to top
View user's profile Send private message Send e-mail
mohrt
Administrator


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

PostPosted: Fri Dec 31, 2004 2:59 pm    Post subject: Reply with quote

Create an array to loop over?

[php:1:8c0a6f1dc5]for ($i=0;3 > $i ;$i++)
{
$tmp = new class_name;
$mystuff[] =& $tmp;
}//END OF FOR

$smarty->assign_by_ref("mystuff",$mystuff);[/php:1:8c0a6f1dc5]

Code:
{section name=foo loop=$mystuff}
   {$mystuff[foo]->func_name()}
{/section}
Back to top
View user's profile Send private message Visit poster's website
bigfishphilip
Smarty n00b


Joined: 09 Nov 2007
Posts: 1

PostPosted: Fri Nov 09, 2007 2:03 am    Post subject: Better late than never Reply with quote

Just came across this looking for the answer myself. I figured it out so thought I might post for future generations...

Code:

{assign var="new_var" value="var_name"}
{assign var=$new_var value="Yes it does"}
{$var_name}

Output is "Yes it does"
Back to top
View user's profile Send private message
chishikinohito
Smarty Regular


Joined: 29 Nov 2010
Posts: 54

PostPosted: Wed Jan 18, 2012 6:22 pm    Post subject: same thing Reply with quote

I was trying to get the same thing work and COULDN'T so far

Can Smarty manage this at all or NOT?

I have a simple idea!!

Could anyone help please:


This one Works:

PHP:

Code:
$categories = Array ( [0] => Array ( [0] => 1 [ID] => 1 [1] => Id's Name [name] => Id's Name [2] => Id's Description [descrip] => Id's Description [3] => [name_ru_RU] => Id's Name in Russian [4] => Id's Name in Russian [descrip_ru_RU] => Id's description in Russian ) [1] => Array (

Template:
Code:

{section name=i loop=$categories}

{if $categories[i].name_ru_RU != NULL && $smarty.session.language == "ru_RU"}

{$categories[i].name_ru_RU}
{$categories[i].descrip_ru_RU}

{else}

{$categories[i].name}
{$categories[i].descrip}


{/section}


BUT

how to make it dynamically work depending on language session? I will have more than 2 languages!

This example doesn't work but it's easy to understand what I'm trying to achieve::

Code:
{if $categories[i].name_$smarty.session.language != NULL && $smarty.session.language == "ru_RU"}


Could anyone help please?
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 -> Smarty Development 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