[Guide] Template Conditionals

Status
Not open for further replies.

Spartan

OMG Member
Jul 10, 2008
337
8
225
40
www.deadlystream.com
I thought it may be handy to have a list of common template conditionals. This isn't a full list, just the most common ones I think must of need at times.

Display different code to guests and members.
Code:
<xen:if is="{$visitor.user_id}">
Code for Members
<xen:else />
Guests
</xen:if>

Display code to user x.
Code:
<xen:if is="{$visitor.user_id}==x">
 Code for user x
 </xen:if>

Display to members only.
Code:
<xen:if is="{$visitor.user_id}">
Code for members
</xen:if>

Display to guests only
Code:
<xen:if is="!{$visitor.user_id}">
Code for guests

</xen:if>

Display if user has more than x posts (replace '>' with '<' for less than x posts).
Code:
<xen:if is="{$visitor.message_count} > x">
Code for members with more/less than x posts

</xen:if>

Display to admins.
Code:
<xen:if is="{$visitor.is_admin}">
FOR ADMINS
</xen:if>

Display to mods.
Code:
<xen:if is="{$visitor.is_moderator}">
FOR MODS
</xen:if>

Display in forum x.
Code:
<xen:if is="{$forum.node_id}==x">
 Code for forum x
 </xen:if>

Display in thread x.
Code:
<xen:if is="{$thread.thread_id}==x">
Code for thread x
</xen:if>

That one isn't common as such but it could be handy, for example you could create a thread with forum rules and then edit the template thread_view with a special notice saying something like 'read carefully' to show in that thread only.

Like I said, that list isn't extensive but it's probably the most common conditionals used.
 
  • Like
Reactions: 7 people

Floris

I'm just me :) Hi.
Staff member
Jan 1, 2001
60,101
1,425
930
47
Netherlands
mrfloris.com
IS USER (browsing) IN USERGROUP? (including additional usergroups) (RC2 and up)

Code:
<xen:if is="{xen:helper ismemberof, $visitor, 5}">
The user browsing is in either primary/secondary usergroup with id 5.
</xen:if>
 

Floris

I'm just me :) Hi.
Staff member
Jan 1, 2001
60,101
1,425
930
47
Netherlands
mrfloris.com
Sorry about the late response, try removing the } in x}">
I think textmate pushed an additional one in there without my permission (ill have to go hit it)
 

Floris

I'm just me :) Hi.
Staff member
Jan 1, 2001
60,101
1,425
930
47
Netherlands
mrfloris.com
SET VARS FOR CONDITIONALS (tnx kier!)

Code:
<xen:set var="$myVar">1234</xen:set>

But you can't set arrays - ideally, that should be done outside the template (view or controller/model layer)
 

Stormraven

Trusted Member
Oct 24, 2010
1,836
0
150
35
Wales, UK
www.prowebforums.com
Thank you Doctor, this could be really helpful to the community, and me for that matter. I haven't played around with Xenforo in depth much as I can't use it on a live site just yet, but when the stable version gets released I will be re-visiting this thread. Thanks for the information :)
 

prodigyfx

OMG Member
Dec 5, 2004
28
0
50
If you want to show an ad in your forum homepage only, just a tiny change to Floris's conditional above
Code:
<xen:if is="{$controllerName} == 'XenForo_ControllerPublic_Index' AND {$controllerAction} == 'Index'"><!-- banner code --></xen:if>

you can put this conditional to your footer or page_container templates.
 
  • Like
Reactions: 1 person

Floris

I'm just me :) Hi.
Staff member
Jan 1, 2001
60,101
1,425
930
47
Netherlands
mrfloris.com
Status
Not open for further replies.