Comparison needed for conditional Generation

04/02/2005

After you have started to modify the Movable Type templates according to your own style, you will probably find out, that this "programming by tags" is a little bit restricted. Often you want to use a condition for creating the HTML output in one or the other way. But Movable Type isn't able to support this very well.

And again, a plugin will do the work.

What do you need Comparisons for?

In this weblog an entry's title area consists of two parts. On the left side there is the title describing the subject. On the right side there is the date of creation.

With some entries I do not want the date to be visible. For example the entry »help« should be without a date.

What is in the template?

The template for creating the title area is basically as follows.

<div class="head">
 <h1 id="a<$MTEntryID pad="1"$>"><$MTEntryTitle$></h1>
 <p><$MTEntryDate format="%A, %B %e, %Y"$></p>
</div>

A div-tag is created. Inside the div an h1-tag is used for the title and a p-tag is used for the date of creation. This is styled by means of CSS, making a blue background appear and the date of creation being shifted to the right.

Our task now seems to be very simple. For suppressing the date of creation we just have to ensure that the third line is ignored in certain situations. In theory this is trivial. But Movable Type doesn't allow that kind of selection.

A Plugin can help

There is the so-called Compare Plugin which extends the tag language extensively. After having installed the plugin, you will be able to code comparisons and also solve the above problem. The plugin is free and easy to install.

Download the plugin with the above link. Then copy the file »compare.pl» into the directory »plugins« below your Movable Type installation. With this simple installation Movable Type now knows the functions contained inside the file.

The plugin creates new tags which can be used for all kind of comparisons. You should study the full list by reading the documentation. I only want to show how the date of creation is suppressed on this website.

If the date is to be suppressed, I enter the value "2005-01-01 00:00:00" into the Movable Type dialog. The field cannot be left empty, so a value which otherwise is not used in this weblog had to be chosen. Within the template the above code is extended as follows.

<div class="head">
 <h1 id="a<$MTEntryID pad="1"$>"><$MTEntryTitle$></h1>
 <MTIfNotEqual a="[MTEntryDate format='%d.%m.%Y']"
               b="01.01.2005">
  <p><$MTEntryDate format="%A, %e. %B %Y"$></p>
 </MTIfNotEqual>
</div>

You will probably recognize that the p-tag for the date of creation is embraced with an MTIfNotEqual-tag. That is the range from the start tag including the two parameters »a« and »b« up to the closing tag. The tag's function is obvious by reading its name: if the two expressions are not equal the inner part will be skipped, so that the date of creation is not output into the resulting HTML file.

The syntax for coding the condition looks strange at first sight. However, it is XML style and fits nicely into the general way of "coding by tags". You hand over two parameters - the left and right hand side of our comparison - to the tag. In this case the right side is a constant.

b="01.01.2005"

And the left side uses the name of an ordinary Movable Type tag.

a="[MTEntryDate format='%d.%m.%Y']"

Evaluating this expression (formatting the date of creation) is initially taken over by the Compare Plugin. In the end the plugin uses other base libraries for this task, but the expression is handed over to the plugin as pure text. Movable Type should not try to evaluate the expression itself. It should leave this task to the plugin. Therefore, the elements have to be modified slightly. The angle brackets were replaced by square brackets, and the double quotes were replaced by single quotes.

Now if I do not want to show the date of creation for an entry, all I have to do is set it to "2005-01-01 00:00:00". The subsequent generation will compose the HTML, but the date will not be copied to the output.

Related Articles

If you found this article usefull, I suggest you also have a look at...

mgs | 04/02/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

Rene | August 5, 2005 04:19 PM

Great site! Thanks for all the info.