|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
alejandrocarg Smarty n00b
Joined: 22 Apr 2012 Posts: 2
|
Posted: Sun Apr 22, 2012 9:58 pm Post subject: Replacing a variable with a variable |
|
|
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 ]
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 |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Mon Apr 23, 2012 6:19 am Post subject: |
|
|
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
which is equivalent to the php
_________________ Twitter |
|
Back to top |
|
alejandrocarg Smarty n00b
Joined: 22 Apr 2012 Posts: 2
|
Posted: Sat Apr 28, 2012 12:23 pm Post subject: |
|
|
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
which is equivalent to the php
|
@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
Thank you very much! |
|
Back to top |
|
|
|
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
|