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

Display block if variable exists

Discussion in 'XenForo Development Discussions' started by Deek, May 20, 2013.

  1. Deek

    Deek Member

    I have some data I'm passing to the view. There is a part where it is doing a foreach loop to generate some selectunit tags. I only want display the content in the loop if the variable exists.

    So in the following I only want this code to be output if $userRoles is set. What are the possible methods of avoiding the undefined variable error when I don't have any $userRoles to loop over?

    <xen:foreach loop="$userRoles" value="$child">
        <xen:selectunit label="{$child.label}" name="options[{$child.name}]">
            <xen:options source="$child.options" />
                <xen:explain>User defined role</xen:explain>
  2. Chris D

    Chris D XenForo Developer Staff Member

    Wrapping that block of code in

    <xen:if is="{$userRoles}">

    And ending in </xen:if> ought to do it.
  3. Deek

    Deek Member

    Derp. I had tried that but I forgot the quotes silly me. Thanks.

Share This Page