Display block if variable exists


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>

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.