Chroder
13th December 2006, 10:46 PM
We all know how powerful the vBulletin template system is. This is, in my opinion, largely due to the fact we can use conditions to fork the template into many different directions. What other software might do with multiple templates or blocks (for example, home_logged_in and home_logged_out), we can do in a single template. But not many people really think about how to use these conditionals other then their intended use.
The vB condition is set up like so:<if condition="PHP Expression Here">
content
<else />
else content
</if>Of course the PHP expression is usually something useful. Like checking to see if a user is logged in, or checking to see if we should show popups. But it can be any PHP expression -- this is what makes it so useful (though note that vBulletin limits what functions can be called for security reasons). Advanced template designers can take advantage of this and perform some nifty tricks. It's been dubbed 'Pseudo Conditionals', using fake conditionals to do something that you might not have been able to do without plugins.
Pseudo Condition: The Assignment
Using a condition to assign a value to a variable is very useful. Here's an example:
<if condition="$the_width = 400"></if>
...
<td width="$the_width">...</td>
<td width="$the_width">...</td>
<td width="$the_width">...</td>
...Sometimes you need to force a width when browsers won't play nice. Instead of hard coding the width in, you can use this pseudo condition to assign the value 400 to $the_width variable, and use the variable within the HTML as you would a real vB variable. Now if you decide to change the 400 to 500 for example, you don't have to change every instance. Can you think of other areas where your own variable could be useful?
Pseudo Condition: The Re-assignment
The above condition shows how you can assign values to your own variables, but you can also reassign values of existing variables. For example, lets say you wanted to change the Username of someone's post in the postbit. (why? I don't know, but I couldn't think of another example!)
<if condition="in_array($post['username'], array('joe','jen','bob'))">
<if condition="$post['username'] = 'Cookie Monster!!!'"></if>
</if>That would make joe's, jen's and bob's usernames appear as 'Cookie Monster!!!' in their posts.
Pseudo Condition: Alternating Rows
Using this conditional in a looping template (that is, a template the is repeated: postbit, threadbit, forumhome_forumbit* etc) you can easily gain alternating rows. For example, take a look at how this threadbit is done:
<if condition="!isset($altrow)">
<if condition="$altrow = 0"></if>
</if>
<if condition="++$altrow % 2 == 0">
<if condition="$altclass = 'alt2'"></if>
<else />
<if condition="$altclass = 'alt1'"></if>
</if>
<tr>
<td class="$altclass" id="td_threadstatusicon_$thread[realthreadid]">
$thread[openclose_editable]
... continue replacing the class with $altclass ...So let's go through this so you can understand what is happening. The first condition is checking to see if $altrow exists (that is what isset() is for). The first time this template is run, it won't exist -- so we need to give it a value of 0 to start it off.
The next condition:++$altrow % 2 == 0First off, the two plus signs before the variable name adds one to the value. So the first time the template is run, $altrow is assigned to 0, and then the plusses make it 1. It would be the same as:$altrow = $altrow + 1The '% 2' means 'get the remainder after I divide the left hand number by 2'. We then compare this value to see if it is 0. '% 2 == 0' basically means 'is the number even', since that is the only time a number can be divided by 2 and have no remainder:
1 % 2 does not equal 0 (1/2, there will be a remainder), so the class will be 'alt1' (the else part of the condition)
2 % 2 does equal 0 (2/2 = 1, no remainder), so the class will be 'alt2'
3 % 2 does not equal 0 (3/2, there will remainder), the class is 'alt1'
4 % 2 does equal 0 (4/2, no remainder), the class is 'alt2'
... etc ...So combining some simple math and the assignment pseudo condition, we gain the ability to easily insert alternating rows.
Pseudo Condition: Every X Rows
Some templates have a counter for you to use in your conditions. For example, a popular template mod is to use the $post[postcount] variable in the postbit to insert ads into threads (http://www.vbulletin.com/forum/showthread.php?t=198875). But if you ever need to do the same kind of thing, but there isn't a counter available (or you just don't feel like finding the name of the variable!), you can use the same kind of technique as above.
<if condition="!isset($mycounter)">
<if condition="$mycounter = 0"></if>
</if>
...
<if condition="++$mycounter % 10 == 0">
This will appear every 10 rows
</if>
Conclusion
I hope you gained some useful knowledge reading this short how-to! If you thought of a different way to use template conditions, don't forget to share with the rest of us.
The vB condition is set up like so:<if condition="PHP Expression Here">
content
<else />
else content
</if>Of course the PHP expression is usually something useful. Like checking to see if a user is logged in, or checking to see if we should show popups. But it can be any PHP expression -- this is what makes it so useful (though note that vBulletin limits what functions can be called for security reasons). Advanced template designers can take advantage of this and perform some nifty tricks. It's been dubbed 'Pseudo Conditionals', using fake conditionals to do something that you might not have been able to do without plugins.
Pseudo Condition: The Assignment
Using a condition to assign a value to a variable is very useful. Here's an example:
<if condition="$the_width = 400"></if>
...
<td width="$the_width">...</td>
<td width="$the_width">...</td>
<td width="$the_width">...</td>
...Sometimes you need to force a width when browsers won't play nice. Instead of hard coding the width in, you can use this pseudo condition to assign the value 400 to $the_width variable, and use the variable within the HTML as you would a real vB variable. Now if you decide to change the 400 to 500 for example, you don't have to change every instance. Can you think of other areas where your own variable could be useful?
Pseudo Condition: The Re-assignment
The above condition shows how you can assign values to your own variables, but you can also reassign values of existing variables. For example, lets say you wanted to change the Username of someone's post in the postbit. (why? I don't know, but I couldn't think of another example!)
<if condition="in_array($post['username'], array('joe','jen','bob'))">
<if condition="$post['username'] = 'Cookie Monster!!!'"></if>
</if>That would make joe's, jen's and bob's usernames appear as 'Cookie Monster!!!' in their posts.
Pseudo Condition: Alternating Rows
Using this conditional in a looping template (that is, a template the is repeated: postbit, threadbit, forumhome_forumbit* etc) you can easily gain alternating rows. For example, take a look at how this threadbit is done:
<if condition="!isset($altrow)">
<if condition="$altrow = 0"></if>
</if>
<if condition="++$altrow % 2 == 0">
<if condition="$altclass = 'alt2'"></if>
<else />
<if condition="$altclass = 'alt1'"></if>
</if>
<tr>
<td class="$altclass" id="td_threadstatusicon_$thread[realthreadid]">
$thread[openclose_editable]
... continue replacing the class with $altclass ...So let's go through this so you can understand what is happening. The first condition is checking to see if $altrow exists (that is what isset() is for). The first time this template is run, it won't exist -- so we need to give it a value of 0 to start it off.
The next condition:++$altrow % 2 == 0First off, the two plus signs before the variable name adds one to the value. So the first time the template is run, $altrow is assigned to 0, and then the plusses make it 1. It would be the same as:$altrow = $altrow + 1The '% 2' means 'get the remainder after I divide the left hand number by 2'. We then compare this value to see if it is 0. '% 2 == 0' basically means 'is the number even', since that is the only time a number can be divided by 2 and have no remainder:
1 % 2 does not equal 0 (1/2, there will be a remainder), so the class will be 'alt1' (the else part of the condition)
2 % 2 does equal 0 (2/2 = 1, no remainder), so the class will be 'alt2'
3 % 2 does not equal 0 (3/2, there will remainder), the class is 'alt1'
4 % 2 does equal 0 (4/2, no remainder), the class is 'alt2'
... etc ...So combining some simple math and the assignment pseudo condition, we gain the ability to easily insert alternating rows.
Pseudo Condition: Every X Rows
Some templates have a counter for you to use in your conditions. For example, a popular template mod is to use the $post[postcount] variable in the postbit to insert ads into threads (http://www.vbulletin.com/forum/showthread.php?t=198875). But if you ever need to do the same kind of thing, but there isn't a counter available (or you just don't feel like finding the name of the variable!), you can use the same kind of technique as above.
<if condition="!isset($mycounter)">
<if condition="$mycounter = 0"></if>
</if>
...
<if condition="++$mycounter % 10 == 0">
This will appear every 10 rows
</if>
Conclusion
I hope you gained some useful knowledge reading this short how-to! If you thought of a different way to use template conditions, don't forget to share with the rest of us.