In this second article, dive a little deeper into theintricacies of DTML by getting up close and personal with conditionalstatements and comparison operators. This article discusses thedifferent variants of the "if" statement available in DTML, togetherwith examples and code listings that demonstrate how they can be used ina Web applications.
For situations whicharen't entirely black and white, DTML also allowsfor multiple conditional branches, via the "if-elif-else" construct. Atypical "if-elif-else" statement block would look like this:
<dtml-if "first condition is true">
do this!
<dtml-elif "second condition is true">
do this!
<dtml-elif "third condition is true">
do this!
... and so on ...
<dtml-else>
do this!
</dtml-if>
And here's an example that demonstrates how to use it. Create a DTMLDocument named "selectDay".
As you can see, this is simply a form which allows you to pick a day ofthe week.
The real work is done by the DTML Method "showMovieForTheDay".
<html>
<head>
<basefont face="Arial">
</head>
<body>
Here is the movie for <dtml-var day> on your favorite channel:
<br>
<dtml-if expr="day == 'Monday'">
<p><b>Dead By Monday</b> - directed by <b>Curt Truninger</b>
<dtml-elif
expr="day == 'Tuesday'"> <p><b>Never On Tuesday</b> - directed by
<b>Adam Rifkin</b></p> <dtml-elif expr="day == 'Wednesday'"> <p><b>Any
Given Wednesday</b> - directed by <b>Neil Mandt</b></p> <dtml-elif
expr="day == 'Thursday'"> <p><b>Thursday Afternoon</b> - directed by
<b>Clay Westervelt</b></p> <dtml-elif expr="day == 'Friday'">
<p><b>Friday The 13th</b> - directed by <b>Sean S. Cunningham</b></p>
<dtml-else> <p><b>Hey, Mr. Couch Potato! It's the weekend...go out
and
get some exercise!</b></p> </dtml-if> <br>
</body>
</html>
In this case, we've used the "if-elif-else" control structure to displaythe appropriate move for the day.
There's one important point to be noted here: as soon as one of the"if"statements within the block is found to be true, Zope will execute thecorresponding code, skip the remaining "if" statements in the block,andjump immediately to the lines following the entire "if-elif-else"block.