pat15 pat15 - 3 months ago 19
PHP Question

Smarty varables concatenation

I have something like this:

<select class="form-control" name="activ">
{if {$smarty.session.admin['access']['pages:list:activ']} == '1'}
<option value="1">{#L_YES#}</option>
<option value="0">{#L_NO#}</option>
{else}
<option value="0">{#L_NO#}</option>
{/if}
</select>


I need to change ['pages:list:activ'] to ['{$smarty.get.module}:list:activ']

How can i do that?

Answer Source

You have multiple options here..

For one, PHP will not parse variables when you are using single quotes.

So

$a = 'string';
echo '$test with something';

Will output:

$test with something

But if you use:

$a = 'string';
echo "$test with something";

or

echo "{$test} with something";

It will output:

string with something

But for Smarty this will not work if you use . or -> in the name, because you will have to use backticks around the variable, like this (note the double quotes around it):

{if {$smarty.session.admin['access']["`$smarty.get.module`:list:activ"]} == '1'}

For more info see: Smarty Embedding Vars in Double Quotes


You can also use cat modifier and assign the concatenated value to a var:

{assign var = "moduleList" value = $smarty.get.module|cat:":list:activ"}
{if {$smarty.session.admin['access'][$moduleList]} == '1'}