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

Template modification to hide content with severals conditional statements

Discussion in 'XenForo Development Discussions' started by Luis, Jun 24, 2014.

  1. Luis

    Luis Well-Known Member

    Hi,
    I'm trying to create a template modification to show or hide content to different groups with severals conditional, I followed this tutorial http://xenforo.com/community/resources/conditional-statements.1604/ but can not get it to work.

    The template to modify is bb_code_tag_code and the conditionals are something like:
    • If the user is a user of the group guest.
      • You need to login or register to view this content.
    • If the user is not a valid user.
      • You need to be a valid user to view this content.
    • If the user is a user of the groups 2 and 6.
      • You need to be a premium user to view this content.
    • The rest of groups may view the content.
    This is the code I'm using is not working for me

    Find
    PHP:
      <pre>{xen:raw $content}</pre><xen:comment><!-- content is escaped via parser --></xen:comment>
    Replace
    PHP:
    <xen:if is="{xen:helper ismemberof, $visitor, 1}">
        <
    div style="padding:10px; text-align: center; font-weight: bold;">
            
    You need to <a href="{xen:link login}">login</a> or <a href="{xen:link register}">register</ato view this content.
        </
    div>
    <
    xen:elseif is="{$visitor.user_state} != 'valid'">
        <
    div style="padding:10px; text-align: center; font-weight: bold;">
            
    You need to be a valid user to view this content.
        </
    div>
    <
    xen:elseif is="{xen:helper ismemberof, $visitor, 2, 5}">   
        <
    div style="padding:10px; text-align: center; font-weight: bold;">
            
    You need to be a premium user to view this content.
        </
    div>
    <
    xen:elseif is="{xen:helper ismemberof, $visitor, 3, 4, 6}">
    $
    0
    </xen:if>
    Thanks and regards, Luis.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Can you clarify what about it isn't working?

    It's most likely the elseif lines:

    Code:
    <xen:elseif is="{xen:helper ismemberof, $visitor, 2, 5}">
    Should be:
    Code:
    <xen:elseif is="{xen:helper ismemberof, $visitor, 2, 5}"/>
    Else If tags do not have a corresponding </xen:elseif> so they are self closing. Without that, the template modification will not parse correctly.
     
    Luis likes this.
  3. Luis

    Luis Well-Known Member

    Many thanks Chris, as you indicated was missing "/" at the end of elseif and also had a confrontation between these two conditions
    PHP:
    <xen:elseif is="{xen:helper ismemberof, $visitor, 2, 5}" />
    ================================================
    <
    xen:elseif is="{xen:helper ismemberof, $visitor, 3, 4, 6}" />
    to fix it I deleted one of them.
    It is now working correctly and the code is:
    PHP:
    <xen:if is="{xen:helper ismemberof, $visitor, 1}">
        <
    div style="padding:10px; text-align: center; font-weight: bold;">
            
    You need to <a href="{xen:link login}">login</a> or <a href="{xen:link register}">register</ato view this content.
        </
    div>
    <
    xen:elseif is="{$visitor.user_state} != 'valid'" />
        <
    div style="padding:10px; text-align: center; font-weight: bold;">
            
    You need to be a valid user to view this content.
        </
    div>
    <
    xen:elseif is="!{xen:helper ismemberof, $visitor, 3, 4, 6}" />   
        <
    div style="padding:10px; text-align: center; font-weight: bold;">
            
    You need to be a premium user to view this content.
        </
    div>
    <
    xen:else />
    $
    0
    </xen:if>
    I'll try to add some option more and if I have any problems I go back. Thanks again.
     
  4. Luis

    Luis Well-Known Member

    Is possible to change this line
    PHP:
    <xen:elseif is="!{xen:helper ismemberof, $visitor, 3, 4, 6}" />
    for something similar to this
    PHP:
    <xen:elseif is="in_array({$user.user_group_id}, array({$xenOptions.MyOption}))" /> 
    I've tried multiple ways but without a satisfactory result, for option I have used a textbox with the data type as string.
    Or maybe it's not possible to do this directly in the template.

    Thanks, Luis.
     
  5. Luis

    Luis Well-Known Member

    Can someone help me with this please.
     
  6. Jeff Berry

    Jeff Berry Well-Known Member

    You would need to first convert your "myOption" string into an array. You will most likely do this in your controller before it reaches the template.

    Example:

    PHP:
    // we do the preg_replace here to make sure there are no spaces in the option
    // so if someone enters "1, 2, 3" it will be converted to "1,2,3"
    $myAllowedGroups preg_replace('\s+'''XenForo_Application::getOptions()->get('myOption'));

    $viewParams = array(
        
    // all the other params you pass to that template
        
    'myAllowedGroups' => explode(','$myAllowedGroups)
    );
    Then in your template you could use (almost) what you already have:
    PHP:
    <xen:elseif is="in_array({$user.user_group_id}{$myAllowedGroups})" /> 
     
    Luis likes this.
  7. Luis

    Luis Well-Known Member

    Thanks for your help Jeff, I'll give it a try and I will inform as things have gone.
     

Share This Page