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

array_key_exists() in template

Discussion in 'XenForo Development Discussions' started by tenants, May 11, 2015.

  1. tenants

    tenants Well-Known Member

    Inside templates, we have a way to check if values exist:

     <xen:if is="in_array({$visitor.user_id}, array('1', '2', '3'))">your id is 1 2 or 3 ...</xen:if>
    but to find if an array key exists is there a similar way?

    I guess usually you could use:

    <xen:if is="{$someVaraible.myKey}"> this key this exists </xen:if>

    But in my case, I need to check if an ip exists in a key, obviously in this case the above wont work, since the key contains decimals:


    currently I'm thinking of looping to check:
       <xen:foreach loop="$someVaraible.myKey" key="$botIp" value="$botTime">
          <xen:if is="{$botIp}==''">Yay, I found my key</xen:if>
    Is there a better (performing and cleaner) way to check to see if this key exists (it's been a while since I've looked at xen template coding, so I'm a bit rusty).

    I was hoping something like this might work:

    <xen:if is="{$someVaraible['myKey']}">
  2. Chris D

    Chris D XenForo Developer Staff Member

    The equivalent of that is

    <xen:set var="$myKey" value="" />
    <xen:if is="{$someVaraible.{$myKey}}">
    tenants likes this.

Share This Page