Template Conditionals 
Template Conditionals are a powerful tool for controlling the XHTML output from your templates. They allow you to create simple if/else branches within your templates, in order to display different content depending on the condition you specify.

For example you may want to show a different welcome message on the front page of your board to registered users and to guests. The way to know whether or not the person visiting a page is a guest, or a logged-in user is to check the value of $bbuserinfo[userid]. If the value is 0, the visitor is a guest (or not logged-in), otherwise the visitor is a registered member.

This is a simple conditional to show a welcome message to guests only.
<vb:if condition="$bbuserinfo['userid'] == 0">
    <p>Welcome to the forum!<br />
    If this is your first visit, we hope you enjoy your stay!</p>
</vb:if>
The previous example used a simple 'if' condition. We can extend that to include an 'else' condition, which will be used if the 'if' condition is not fulfilled.

This example extends the previous conditional to show a different message to registered members from that shown to guests.
<vb:if condition="$bbuserinfo['userid'] == 0">
    <p>Welcome to the forum!<br />
    If this is your first visit, we hope you enjoy your stay!</p>
<vb:else />
    <p>Welcome back, $bbuserinfo[username].<br />
    <a href="search.php?do=getnew">Click here to view new posts</a>.
</vb:if>
Starting in vBulletin 4.0 template conditionals natively support 'else if' as well.
<vb:if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
<vb:elseif condition="$my_variable == 2" />
    <p>My variable is equal to two.</p>
<vb:else />
    <p>My variable is equal to neither one nor two.</p>
</vb:if>
The actual syntax of vBulletin template conditionals is fairly straight forward. To begin a conditional, you simply start an <vb:if> tag. The <vb:if> tag accepts a single attribute, that being 'condition'. The value of the condition attribute contains an expression written in PHP. After the opening <vb:if> tag comes the HTML that should be expressed if the condition is met. The conditional terminates with a closing </vb:if> tag.

Perhaps the easiest way to illustrate this is to demonstrate a simple example of PHP code being embedded as a template conditional.

Let us assume for the purposes of this example that we want to have the equivalent of this PHP code in our template:
if ($my_variable == 1)
{
    echo 
"<p>My variable is equal to one.</p>";

This could be expressed as a template conditional in the following way:
<vb:if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
</vb:if>
If we were to extend our PHP code to include an 'else' condition as follows...
if ($my_variable == 1)
{
    echo 
"<p>My variable is equal to one.</p>";
}
else
{
    echo 
"<p>My variable is not equal to one.</p>";

... then our template conditional would be extended thus:
<vb:if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
<vb:else />
    <p>My variable is not equal to one.</p>
</vb:if>
Furthermore, we may want to extend our PHP with an 'else if' condition:
if ($my_variable == 1)
{
    echo 
"<p>My variable is equal to one.</p>";
}
else if (
$my_variable == 2)
{
    echo 
"<p>My variable is equal to two.</p>";
}
else
{
    echo 
"<p>My variable is equal to neither one nor two.</p>";

<vb:if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
<vb:elseif condition="$my_variable == 2" />
    <p>My variable is equal to two.</p>
<vb:else />
    <p>My variable is equal to neither one nor two.</p>
</vb:if>
User Contributed Notes: Template Conditionals Add a Comment
Dominik Hahn 20th Apr 2005, 11:03am
Something like
if(($moo == '1') AND ($baa == '0'))
{
echo "boo";
}


can be done with
<if condition="($moo == 1) AND ($baa == 0)">
boo
</if>
Colin 20th Apr 2005, 11:06am
For anyone that's interested, template conditionals do _not_ result in additional querys.
Jordon Bedwell 20th Apr 2005, 11:11am
Shouldnt:

<if condition="$bbuserinfo['userid'] == 0">
<p>Welcome to the forum!<br />
If this is your first visit, we hope you enjoy your stay!</p>
<else />
<p>Welcome back, $bbuserinfo[username].<br />
<a href="search.php?do=getnew">Click here to view new posts</a>.
</if>



Be:

<if condition="$bbuserinfo[userid] == 0">
<p>Welcome to the forum!<br />
If this is your first visit, we hope you enjoy your stay!</p>
<else />
<p>Welcome back, $bbuserinfo[username].<br />
<a href="search.php?do=getnew">Click here to view new posts</a>.
</if>


I got a parse error when it had those in it :S
Mike Gorham 20th Apr 2005, 11:11am
Note that you can use a global variable in a conditional statement this way:
<if condition="$GLOBALS['NoShowLoginPopup'] = 1">

But if you want to print out the global variable using the GLOBALS array in a template, use this syntax:

{$GLOBALS['NoShowLoginPopup']}
Colin 17th May 2005, 10:56am
I've found as well that array variables should be saved without the apostrophes ($bbuserinfo[userid] instead of $bbuserinfo['userid'] for example).
The second gives a parse error.
Selrion 19th Feb 2008, 05:02pm
There are no any differences between this code:

<if condition="($myvar_1 == 1) AND ($myvar_2 == 0)">
Operators
</if>

And the following code:

<if condition="$myvar_1 == 1 AND $myvar_2 == 0">
Operators
</if>

Both ones executes fine. Please note that you can use logical operators like this:

<if condition="($myvar_1 == 1) || ($myvar_2 == 0)">
Operators
</if>

Or:

<if condition="$myvar_1 == 1 || $myvar_2 == 0">
Operators
</if>
Skavenger 24th Jul 2008, 06:18am
check this: http://www.vbulletin.com/forum/showthread.php?t=200894&highlight=conditionals
Ben Shelock 18th Feb 2009, 11:03am
Very good article here. Helps alot.

http://forum.vbulletinsetup.com/f18/vbulletin-template-conditionals-list-2185.html
Daniel Lemon 29th Jun 2009, 10:26pm
Ok, I spent at least 5 hours trying to figure out how to query if the poster is in a certain usergroup instead of the user browsing the thread. Thanks to Twitter, @lincolnwebs -> who found it here, for me:
http://www.vbulletin.com/forum/showthread.php?p=1728240#post1728240

To find out if the user browsing the forum is in a certain usergroup then use this:
<if condition="is_member_of($bbuserinfo,2,8,10,11) ">

To find out if the poster is in a certain usergroup then use this:
<if condition="is_member_of($post,2,8,10,11)">
nlwin 10th Nov 2010, 08:15am
If ElseIf If example doesn't work in vb4.

I had to do as below:

<vb:if condition="$show['threadinfo'] == 1">
<p>threadinfo</p>
<vb:else />
<vb:if condition="$show['foruminfo'] == 1">
<p>foruminfo</p>
<vb:else />
<p>forumhome</p>
</vb:if>
</vb:if>
Add a Comment