View Full Version : Using Pseudo (Fake) Conditionals


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.

darnoldy
14th December 2006, 03:12 AM
chroder-

Very cool!

PayBas
29th December 2006, 02:45 PM
Very good info :) but I'm having trouble setting it up:

This is the beginning of my "postbit_legacy" template


<!-- Alternating Row Config -->
<if condition="!isset($altrow)">
<if condition="$altrow = 0"></if>
</if>
<if condition="++$altrow % 2 == 0">
<if condition="$postshell00 = 'postshell21'"></if>
<if condition="$postingcontainer00 = 'postingcontainer21'"></if>
<if condition="$posttable00 = 'posttable21'"></if>
<else />
<if condition="$postshell00 = 'postshell11'"></if>
<if condition="$postingcontainer00 = 'postingcontainer11'"></if>
<if condition="$posttable00 = 'posttable11'"></if>
</if>
<!-- / Alternating Row Config -->


And further along in the template i have:
<div id="$postshell00">blablabla</div>
<div class="$postingcontainer00">blablabla</div>
<table id="$posttable00">blablabla</table>

Now I keep getting the first alternative, it doesnt alternate :(. So i constantly get the <else /> entries.

Any input would be appreciated.

PayBas
31st December 2006, 07:45 PM
*bump* anyone?

Chroder
31st December 2006, 11:47 PM
<!-- Alternating Row Config -->
<if condition="$altrow =& $GLOBALS['altrow']"></if>
<if condition="!isset($altrow)">
<if condition="$altrow = 0"></if>
</if>
<if condition="++$altrow % 2 == 0">
<if condition="$postshell00 = 'postshell21'"></if>
<if condition="$postingcontainer00 = 'postingcontainer21'"></if>
<if condition="$posttable00 = 'posttable21'"></if>
<else />
<if condition="$postshell00 = 'postshell11'"></if>
<if condition="$postingcontainer00 = 'postingcontainer11'"></if>
<if condition="$posttable00 = 'posttable11'"></if>
</if>
<!-- / Alternating Row Config -->You spotted an issue I didn't anticipate :p It seems the variables are re-set in some cases. In this case I'm assuming a function is called to render each postbit instead of all of them in a loop, so the variables are reset each time due to loosing scope.

To rectify the issue, you can make it so the counter variable is a global variable as I have outlined in red.

The =& means 'by reference', so whatever happens to the local variable will also happen to the global one so the changes are kept for the next post.

PayBas
2nd January 2007, 01:20 AM
WORKS! :)

/me bows

Ambient Decay
16th May 2007, 11:19 AM
Wow thanks for the short how-to. It's nice to see the around on the boards. I did learn something new, but then again I'm still relatively new to using PhP. Thanks again.:D

Edder
24th May 2007, 11:34 PM
Very neat tutorial - I will have to check this out sometime..