DTML Basics (part 2) - Celluloid Dreams (
Page 5 of 8 )
For situations which
aren't entirely black and white, DTML also allows
for multiple conditional branches, via the "if-elif-else" construct. A
typical "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 DTML
Document named "selectDay".
<html>
<head>
<style type="text/css">
td {font-family: Arial;}
</style>
</head>
<body>
<font face="Arial" size="+2">
Movie Schedule
</font>
<form method="POST" action="showMovieForTheDay">
<table cellspacing="5" cellpadding="5" border="0">
<tr>
<td align="center">
Pick a day
</td>
<td align="right">
<select name="day">
<option value="Monday">Monday
<option value="Tuesday">Tuesday
<option value="Wednesday">Wednesday
<option value="Thursday">Thursday
<option value="Friday">Friday
<option value="Saturday">Saturday
<option value="Sunday">Sunday
</select>
</td>
</tr>
<tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Hit me!">
</td>
</tr>
</table>
</form>
</body>
</html>
As you can see, this is simply a form which allows you to pick a day of
the 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 display
the 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 the
corresponding code, skip the remaining "if" statements in the block,
and
jump immediately to the lines following the entire "if-elif-else"
block.