Template syntax: variable as array key

refael

Well-known member
I need to make a condition that use a variable as the array key: $arr[$var]
Couldn't find the right template syntax for that.

Few ways that I've tried and doesn't worked
{xen:if $arr.$var, 'my content..'}
{xen:if $arr.{$var}, 'my content..'}
{xen:if $arr.{xen:raw $var}, 'my content..'}
 
Try this:

{xen:if '{$arr.{$var}}', 'my content..'}

It may not work, though. I seem to recall having similar issues. But certainly single quotes around it and ensuring the main {$arr} variable has curly brackets around it won't hurt.
 
Try this:

{xen:if '{$arr.{$var}}', 'my content..'}

It may not work, though. I seem to recall having similar issues. But certainly single quotes around it and ensuring the main {$arr} variable has curly brackets around it won't hurt.
Thanks!, the curly brackets fixed the thing.

Any suggestion how to combine a string? $arr[$var.'_test']

For now I workaround it with xen:set
PHP:
<xen:set var="$temp">{xen:raw var}_test</xen:set>
{xen:if {$arr.{$temp}}, 'my content..'}

It works, but one line code would be more elegant.
 
That might be where single quotes come in handy...

{xen:if '{$arr.{xen:raw $var}_test}', 'my content..'}
 
Back
Top Bottom