Template modification to hide content with severals conditional statements

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</a> to 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.
 
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:
Rich (BB 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.
 
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</a> to 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.
 
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.
 
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.
Can someone help me with this please.
 
Can someone help me with this please.
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})" />
 
Top Bottom