Home arrow Zope arrow Page 5 - DTML Basics (part 2)

Celluloid Dreams - Zope

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.

TABLE OF CONTENTS:
  1. DTML Basics (part 2)
  2. If Only...
  3. Comparing Apples And Oranges
  4. Welcome To The Matrix
  5. Celluloid Dreams
  6. Paring It Down
  7. Deeper And Deeper
  8. Submitting To The King
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 4
June 05, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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".

<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 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.

 
 
>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

ZOPE ARTICLES

- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: