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

xen:if hascontent

Discussion in 'XenForo Development Discussions' started by ragtek, Jan 11, 2011.

  1. ragtek

    ragtek Guest

    How das hascontent work?

    This is my template:
    Code:
    <table>
    <xen:foreach loop="$invitedUsers" value="$user">
    <!-- todo maka a own template for this -->
    <tr>
    <td>{$user.invited_mail}</td>
    </tr>
    </xen:foreach>
    </table>
    
    Can i use the hascontent check, to show the table IF $invitedUsers has entries, and if it's empty to show something different?

    I've tried:
    Code:
    <xen:if hascontent="true">
    <xen:contentcheck>
    <table>
    <xen:foreach loop="$invitedUsers" value="$user">
    <!-- todo maka a own template for this -->
    <tr>
    <td>{$user.invited_mail}</td>
    </tr>
    </xen:foreach>
    </table>
    </xen:contentcheck>
    </xen:if>
    but the code is very strange and i don't know how to make the else part^^

    Is it possible to make this check in the template, or do i have to make this with a viewparam ($haveInvites = true;)
     
  2. Mike

    Mike XenForo Developer Staff Member

    The <xen:if hascontent="true"> will evaluate to true, if (when processing the template), the contents within <xen:contentcheck> contain a non-whitespace character. The contentcheck must be a direct child of the if (in terms of other <xen:*> tags). You can pair it with an else, though that rarely happens.

    Generally speaking, you can structure your code to not need it, but it can make things simpler. Consider something like:

    HTML:
    <xen:if hascontent="true">
    <ul>
    <xen:contentcheck>
    	<xen:if is="{$var1}"><li>1</li></xen:if>
    	<xen:if is="{$var2}"><li>2</li></xen:if>
    	<xen:if is="{$var3}"><li>3</li></xen:if>
    	<xen:if is="{$var4}"><li>4</li></xen:if>
    	<xen:if is="{$var5}"><li>5</li></xen:if>
    	<xen:if is="{$var6}"><li>6</li></xen:if>
    </xen:contentcheck>
    </ul>
    </xen:if>
    Effectively, the ul will only be displayed if there is something to put inside it. The alternative would be to wrap the whole thing in <xen:if is="{$var1} OR {$var2} ...">, which is a pain.
     
  3. ragtek

    ragtek Guest

    AH, thx:)
    So if all checks are false, the ul will not be displayed.


    Nice feature:)
     

Share This Page