[Guide] Template Conditionals

Status
Not open for further replies.

Spartan

OMG Member
Joined
Jul 10, 2008
Messages
337
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.
 

imported_Brian

OMG Member
Joined
Oct 6, 2010
Messages
161
Additionally, you can also set conditional for Administrators only

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

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100
Very cool guide, I've tweeted this one out. I think it's really nice of you to publish this on the xenfans site. Thumbsup!
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100
FIRST POST OF THREAD

Code:
<xen:if is="{$post.position} == 0">
.. do something in the first post ..
</xen:if>
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100
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
Joined
Jan 1, 2001
Messages
60,100
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
Joined
Jan 1, 2001
Messages
60,100
BAH! floris :(

Ok, i've updated the post, and fixed it.. sorry about that.
 

wisemasterchief

OMG Member
Joined
Feb 26, 2011
Messages
64
Sample code where conditional is determined from templates

Code:
<xen:if is="{$contentTemplate} == 'EWRporta_Portal'">

        <xen:include template="Board_Message_By_Kaiser" />

</xen:if>
 

wisemasterchief

OMG Member
Joined
Feb 26, 2011
Messages
64
Node selection that can be altered to an EQUAL TO or a NOT EQUAL TO conditional. This would also work nicely for a register page.

Code:
<xen:if is="{$quickNavSelected} != 'node-3'">

   YOUR CODE GOES HERE

</xen:if>
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100
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
Joined
Oct 24, 2010
Messages
1,836
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
Joined
Dec 5, 2004
Messages
28
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.
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100
<xen:if is="{$forum.node_id} == 2"> test </xen:if>
 

Vincent_imported

Trusted Member
Joined
Dec 1, 2010
Messages
747
<xen:if is="{$forum.node_id} == 2"> test </xen:if>

That's not what I meant :s
Let's say it in another way.

How can I, in one template that appears on more then 1 page, show at page "member profile" content X and at another page show content Y.
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100

PoLiZe_imported

OMG Member
Joined
Apr 7, 2011
Messages
4
Great, i can translate that and put on "XenFácil" (Spanish XenForo Support Forum) with the source link (here)?
 

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,100
Great, i can translate that and put on "XenFácil" (Spanish XenForo Support Forum) with the source link (here)?
Please see my other reply in the other thread.
 
Status
Not open for further replies.
Top