• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Template syntax: variable as array key

rellect

Well-known member
#1
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..'}
 

Chris D

XenForo developer
Staff member
#2
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.
 

rellect

Well-known member
#3
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.
 

Chris D

XenForo developer
Staff member
#4
That might be where single quotes come in handy...

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