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

Is there a reason to use hascontent instead of just an is check?

Discussion in 'XenForo Development Discussions' started by Jaxel, Jan 13, 2014.

  1. Jaxel

    Jaxel Well-Known Member

    Code:
    <xen:if hascontent="true">
        <ul class="adminList">
            <xen:contentcheck>
            <xen:foreach loop="$admins" value="$user">
                <li><xen:username user="$user" rich="true" /></li>
            </xen:foreach>
            </xen:contentcheck>
        </ul>
    </xen:if>
    
    VS
    Code:
    <xen:if is="{$admins}">
        <ul class="adminList">
            <xen:foreach loop="$admins" value="$user">
                <li><xen:username user="$user" rich="true" /></li>
            </xen:foreach>
        </ul>
    </xen:if>
    
    Why choose the content check instead of just doing the if is?
     
  2. Brogan

    Brogan XenForo Moderator Staff Member

  3. jmurrayhead

    jmurrayhead Well-Known Member

    I used this for replacing the moderatorBar with a generic userBar. It shows the bar if there is content to be shown, regardless if regular user or admin/moderator.
     
  4. Jaxel

    Jaxel Well-Known Member

    Yes, I know that... I know of the many cases in which hascontent is great... I use it in all my mods...

    What I'm asking is, in this SPECIFIC CASE... is there a reason to use hascontent? I see a lot in the official templates that Kier uses hascontent, instead of just using a regular IF... I want to know why.
     
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    Well, in the example you quoted, not using hascontent would duplicate conditionals that you have to manage. One to show if at least one is present, and then once again for each individual item. hascontent reduces that so you only have to manage each inner conditional once.
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    I was about to say pretty much the same as Jeremy.

    Here's a good explanation from Mike about it:

    http://xenforo.com/community/threads/xen-if-hascontent.10317/#post-140380

    In general, in your specific case, both are valid and neither has a particular benefit over the other. But certainly in the example that Mike gives, it's clear that hascontent would be better.
     
    Jeremy likes this.

Share This Page