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

XF 1.0 Conditionals for different usergroups

Discussion in 'XenForo Questions and Support' started by Morgain, Nov 20, 2012.

  1. Morgain

    Morgain Well-Known Member

    My first effort at directing 3 different usergroups to 3 appropriate links.

    Code:
    <li><xen comment>IF FIRST</xen comment><xen:if is="{$visitor.user_id} == 8"><a href="mysite.10/">{xen:phrase  blah}</a></xen:if>
    <xen comment>IF SEN</xen comment><xen:if is="{$visitor.user_id} == 9"><a href="mysite.49/">{xen:phrase  blah}</a></xen:if>
    <xen comment>IF SCHOL</xen comment><xen:if is="{$visitor.user_id} == 7"><a href="mysite.5/">{xen:phrase blah}</a></xen:if>
    </li>
    I get error that - Template tags are not well-formed. <xen:if> was not closed.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Nice try!

    Firstly, {$visitor.user_id} is the user ID of the current visitor. Not the usergroup. So this won't work.

    There is {$visitor.user_group_id} but that only contains the primary usergroup ID. There is also {$visitor.secondary_group_ids} but that is a string rather than an array so that's difficult to work with.

    Thankfully, there's a nice way of handling this. XenForo created a template helper for this... There might also be a different way of doing your if statements too... but based on your example:

    Code:
    <li>
    	<xen:if is="{xen:helper ismemberof, $visitor, 8}">
    		<a href="mysite.10/">{xen:phrase blah}</a>
    	</xen:if>
    	<xen:if is="{xen:helper ismemberof, $visitor, 9}">
    		<a href="mysite.49/">{xen:phrase blah}</a>
    	</xen:if>
    	<xen:if is="{xen:helper ismemberof, $visitor, 7}">
    		<a href="mysite.5/">{xen:phrase blah}</a>
    	</xen:if>
    </li>
    
    An alternate way of doing if statements...

    With the above, if a member is part of usergroups 8, 9 AND 7 then they will see all three links. Is that intentional?

    You could do this:

    Code:
    <li>
    	<xen:if is="{xen:helper ismemberof, $visitor, 8}">
    		<a href="mysite.10/">{xen:phrase blah}</a>
    	<xen:elseif is="{xen:helper ismemberof, $visitor, 9}" />
    		<a href="mysite.49/">{xen:phrase blah}</a>
    	<xen:elseif is="{xen:helper ismemberof, $visitor, 7}" />
    		<a href="mysite.5/">{xen:phrase blah}</a>
    	</xen:if>
    </li>
    
    Notice the inclusion of elseif... If the user is a member of groups 8, 9 and 7 they will only see the link for group 8 (the first one matched). You can rearrange the order however you like to get a different result.

    Either way, good luck :)

    One last note:

    Can't see that the <xen:if> tags aren't closed... but the comment tags aren't correct. They should be <xen:comment> rather than <xen comment>
     
    Morgain, Jake Bunce and Syndol like this.
  3. Morgain

    Morgain Well-Known Member

    Thank you immensely for your very generous help. I got it to work and I am learning lots from what you showed me. You're a sweetheart.
     
    Chris D likes this.

Share This Page