Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Replacing a variable with a variable

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
alejandrocarg
Smarty n00b


Joined: 22 Apr 2012
Posts: 2

PostPosted: Sun Apr 22, 2012 9:58 pm    Post subject: Replacing a variable with a variable Reply with quote

Hi there, first post :]

I have something like this all along one of my scripts

{$a.hello.id}
or sometimes..
{if $a.hello.id ... }

I also have a small if now, at the top of my script, checking if we need the info of $a or $b.
So sometimes, I might need..
{$a.hello.id}
and sometimes, I might need..
{$b.hello.id}

I've searched the forum, and found a nice, working way to make a variable variable, like this:

{assign var="new_var" value="var_name"}
{assign var=$new_var value="I could variate"}
{$var_name}
Output is "I could variate"

by bigfishphilip [thanks Wink]



Now the problem is replacing the statements. I've tried many things, including backticks, double quotes, etc. without any success.

It should look something like this:

$var_holding_replacement is 'b'

So I replace all the

{$a.hello.id}
with something like
{$'$var_holding_replacement'.hello.id}

and on render, it should look like
{$b.hello.id}



Any help would be appreciated :]

PD: sometimes, I won't need exactly .hello.id, so the only thing that MUST change is only the starting word.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Mon Apr 23, 2012 6:19 am    Post subject: Reply with quote

I believe you'd be better off if you did something like this instead:

Code:
{* determine what you need to work with *}
{if $some_condition}
  {$z = $a}
{else}
  {$z = $b}
{/if}

{* work it *}
{$z.hello.id}


Anyways, what you were asking for is
Code:
{$foo_{$bar}}

which is equivalent to the php
Code:
${"foo_$bar"}

_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
alejandrocarg
Smarty n00b


Joined: 22 Apr 2012
Posts: 2

PostPosted: Sat Apr 28, 2012 12:23 pm    Post subject: Reply with quote

rodneyrehm wrote:
I believe you'd be better off if you did something like this instead:

Code:
{* determine what you need to work with *}
{if $some_condition}
  {$z = $a}
{else}
  {$z = $b}
{/if}

{* work it *}
{$z.hello.id}


Anyways, what you were asking for is
Code:
{$foo_{$bar}}

which is equivalent to the php
Code:
${"foo_$bar"}


@rodneyrehm, thanks for your reply!

As you said, it was as simple as your first example. I was doing a mistake by stating $z = b, and not $z = $b. My bad ;[

There's this one thing though; one of the values (say $b) I'm passing is an array. Something like $b[key]. I'm not being able to get that through. I've tried backticks without any success either ($z = "`$b[key]`"). $a is working fine though.

http://www.smarty.net/docsv2/en/language.syntax.quotes.tpl
{func var="test $foo[bar] test"} <-- sees $foo[bar]

Any thoughts?

Thanks in advance :]


Nevermind, I was puting the code outside the loop. Problem solved Smile

Thank you very much!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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