Reputation: 41
I have this:
string {$one} = "$hello_"
string {$two} = "world"
How can I call the variable $hello_world
from the above two string variables?
capture
did not work for me.
Uses Smarty v2.5
Upvotes: 2
Views: 1802
Reputation: 27017
From the documentation:
{$foo_{$x}} // will output the variable $foo_1 if $x has a value of 1.
So, you want:
{${$one}{$two}}
Since this functionality isn't allowed, I would recommend using a smarty plugin to mimic the behavior you want. Template plugins are just simple php functions, called via the $smarty->loadPlugin()
method.
Upvotes: 1
Reputation: 33197
Smarty 2.x doesn't support variable variables.
Variables in smarty are actually stored inside the smarty object so you'd need explicit support in Smarty to use the convenient standard variable-variable syntax.
The following is the best I could come up with in Smarty 2.x. It uses a PHP block to store the value of the combined result for you.
{assign var="one" value="hello_"}
{assign var="two" value="world"}
{assign var="hello_world" value="HELLO!"}
{php}
$varname = $this->get_template_vars('one').$this->get_template_vars('two');
$this->assign('result', $this->get_template_vars($varname));
{/php}
{$result}
As mentioned, however, you have to get rid of the $
at the beginning of the value of $one
. Otherwise you will need to modify it to the following line:
$varname = substr($this->get_template_vars('one'),1).$this->get_template_vars('two');
Upvotes: 0
Reputation: 13557
{${$foo}{$bar}} will only work in Smarty3, though. In Smarty2 you'd have to write a plugin for that (or simply search the smarty forum, as there are plenty of solutions there…)
Upvotes: 1