Build Conditions with the Compare Plugin

05/09/2005

In Comparison needed for conditional Generation I showed how to use the Compare Plugin for managing variations of a page type. The plugin allows comparing values and optionally inserting one or the other code segment into the output stream. However, the simple condition "if one value equals another value" is just the beginning. We are also able to create complex conditions. It is not even that difficult.

Basics

The Compare Plugin offers a number of tags, as for example MTIfEqual, MTIfNotEqual, MTIfGreater, MTIfGreaterOrEqual, MTIfLess, etc. Most of them work the same way: they take at least two mandatory attributes, a and b. The plugin compares the values of those two attributes, and only if the comparison evaluates to true, the embraced lines are included into the generated file.

Let us start with two simple samples.

<MTIfEqual a="1" b="2">
<p>This will never be seen.</p>
</MTIfEqual>

The above lines shows a comparison, where the two strings "1" and "2" are compared. Of course, they are not equal. So the inner part of the tag will never be evaluated.

<MTIfEqual a="1" b="1">
<p>This will always be seen.</p>
</MTIfEqual>

The above shows a comparison, where the two strings "1" and "1" are compared. Of course, they are always equal. So the inner part of the tag will always be evaluated.

It is not reasonable to compare two constant values this way. I just wanted to show the basic structure for using the tags. In FAQ: How to hide a Category from the Start Page I discussed a way for selectively showing categories in an index template. Let us use that scenario for the following samples.

We can create a list of all category names as follows.

<ul>
<MTCategories>
<li><MTCategoryLabel></li>
</MTCategories>
</ul>

If we want to exclude the category "Hardware" from this list, we would write the following.

<ul>
<MTCategories>
<MTIfNotEqual a="[MTCategoryLabel]" b="Hardware">
<li><MTCategoryLabel></li>
</MTIfNotEqual>
</MTCategories>
</ul>

We can see that the two values for the Compare Plugin may not only be constant values. They can also be tags. However, the syntax for using the tags is slightly different: we have to replace all angle brackets by square brackets, and all double quotes by single quotes.

Condition AND Condition

Suppose you wanted to create a list of all categories. However, both the category "Hardware" and "Software" are to be excluded. The solution is simple.

<ul>
<MTCategories>
<MTIfNotEqual a="[MTCategoryLabel]" b="Hardware">
<MTIfNotEqual a="[MTCategoryLabel]" b="Software">
<li><MTCategoryLabel></li>
</MTIfNotEqual>
</MTIfNotEqual>
</MTCategories>
</ul>

Here we nest two comparisons within each other. The flow of control will only pass over first comparison, if the current category is not "Hardware". Then, the second comparison makes sure that the current category is not "Software". Effectively, the li-tag will only be output, if the category is neither "Hardware" nor "Software".

Adding another comparison is done straight forward. We just have to nest all further comparisons inside the existing structure.

Condition OR Condition

In the previous sample, I showed how to code an AND-condition. The OR-condition is a little bit more difficult. What should we do, if we want to specify "do something, if one or the other is true"?

We have to use an extra variable for that. As you probably know, the Movable Type Tag Language allows using variables. You can assign a value to a variable and retrieve the value from the variable later. You use the MTSetVar and MTGetVar tags for this task.

<MTSetVar name="mgs_var" value="mgs for president">
<MTSetVar name="mgs_var" value="better not">

<MTGetVar name="mgs_var">

The above lines assign a value to a variable twice and then later use the value. The second assignment overwrites the value inside the variable.

If we wanted to express "do something, if the category is either Hardware or Software", we might use the following lines.

<ul>
<MTCategories>

<MTSetVar name="mgs_condition" value="0">
<MTIfEqual a="[MTCategoryLabel]" b="Hardware">
   <MTSetVar name="mgs_condition" value="1">
</MTIfEqual>
<MTIfEqual a="[MTCategoryLabel]" b="Software">
   <MTSetVar name="mgs_condition" value="1">
</MTIfEqual>

<MTIfEqual a="[MTGetVar name='mgs_condition']" b="1">
<li><MTCategoryLabel></li>
</MTIfEqual>

</MTCategories>
</ul>

We use the variable "mgs_condition" for calculating the result. Initially this variable is set to "0". Then, if the category name is "Hardware", it is set to "1". Next, if the category name is "Software", it is set to "1". After these lines, the variable has a value of "1", if the category is either "Hardware" or "Software".

The final comparison looks a little bit strange at first sight.

<MTIfEqual a="[MTGetVar name='mgs_condition']" b="1">

The left side of the comparison is a Movable Type tag. It is the MTGetVar tag, which we use for extracting the variable's value. We just have to regard the rule, that all angle brackets have to be replaced by square brackets, and all double quotes have to be replaced by single quotes.

Now the above lines make sense. They read "extract the value from the variable mgs_condition and if the value is 1 then do the following".

Combined Conditions

If we further complicate the condition, this will not make the coding more difficult. It will only be longer. You have to split the condition into several parts, calculate each of them independently, save the intermediate result in some variable, and finally combine all variables.

mgs | 05/09/2005

Feedback is welcome!

What do you think about this entry? Was it interesting or boring? I would like to hear your comments. If the text was helpful, please consider setting a link to http://www.movable-type-weblog.com/.

No spam please!

For protecting this weblog I have installed the MT-Approval Plugin. You have to view a new comment in preview mode, before it is saved on the server. Moreover, I will view your comment manually, before it is published. You can find more information on the subject in the entry Weblog Spamming Basics.

With an active TypeKey session, your comment will be published immediately.

Post a new comment

TypeKey has temporarily been disabled at this location. Please create your comment without using TypeKey or log in from the preview dialog.




Remember Me?


Comment

Toni | May 21, 2005 03:33 PM

I like the trouble you've gone to with your site. It is very informative and looks great. I've added you to my Blogroll and have syndicated the site. Keep up the great work.

Comment

Ivan Pellegrin | July 14, 2006 06:58 PM

Thank you for explaining all of this very clearly. I can now use this tag with confidence.