1. 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

Discussion in 'XenForo Development Discussions' started by rellect, Jun 15, 2013.

  1. rellect

    rellect 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..'}
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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 likes this.
  3. rellect

    rellect Well-Known Member

    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.
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    That might be where single quotes come in handy...

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

    rellect Well-Known Member

    Doesn't work.
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    You may just have to do it how you were doing it, then.
     

Share This Page