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.
<if condition="$bbuserinfo['userid'] == 0">
    <p>Welcome to the forum!<br />
    If this is your first visit, we hope you enjoy your stay!</p>
</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.
<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>
The actual syntax of vBulletin template conditionals is fairly straight forward. To begin a conditional, you simply start an <if> tag. The <if> tag accepts a single attribute, that being 'condition'. The value of the condition attribute contains an expression written in PHP. After the opening <if> tag comes the HTML that should be expressed if the condition is met. The conditional terminates with a closing </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:
<if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
</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:
<if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
<else />
    <p>My variable is not equal to one.</p>
</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>";

vBulletin template conditionals do not natively support 'else if', but you can easily replicate its behavior by nesting conditionals as follows:
<if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
<else />
    <if condition="$my_variable == 2">
        <p>My variable is equal to two.</p>
    <else />
        <p>My variable is equal to neither one nor two.</p>
    </if>
</if>
User Contributed Notes: Template Conditionals Add a Comment
Dominik Hahn <mrnase at sportboard dot de> Apr 20th '05, 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 <colin dot frei at vbulletin dot com> Apr 20th '05, 11:06am
For anyone that's interested, template conditionals do _not_ result in additional querys.
Mike Gorham <mgorham at mtbr dot com> Apr 20th '05, 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']}
Jordon Bedwell <xpresources at sbcglobal dot net> Apr 20th '05, 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
Colin <colin dot frei at vbulletin dot com> May 17th '05, 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 <anky at km dot ru> Feb 19th '08, 04: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 <leandro dot ska at gmail dot com> Jul 24th '08, 06:18am
check this: http://www.vbulletin.com/forum/showthread.php?t=200894&highlight=conditionals
Add a Comment