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

Comparing Apples And Oranges - 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.

  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


Theexample you've just seen was very rudimentary. To really add somepunch, you need to know how to construct what the geeks call aconditional statement. And the very basis of a conditional statement iscomparison - for example, "if this is equal to that, do thus and such".

Since Zope is built around Python, and Python comes with a bunch ofuseful operators designed specifically for use in conditionalstatements, you can use Python operators to build conditional statementsin your DTML. Here's alist:

Assume delta = 12 and omega = 9

Operator What It Means Expression
Evaluates To

== is equal to delta == omega False

!= is not equal to delta != omega True

is greater than delta > omega True

< is less than delta < omega False

= is greater than or equal to delta >= omega True

<= is less than or equal to delta <= omega False
These comparison operators can be used for both strings and numbers. Apositive result returns true (1), while a negative result returns false(0).

Here's an example, a DTML Method named "access", which illustrates howthese can be used:

<dtml-if expr="name == 'neo'">
<font face="Arial" size="-1">
Welcome to the Matrix, Neo. Access granted.

<dtml-if expr="name != 'neo'">
<font face="Arial" size="-1">
I wonder if you've heard of Shakespeare, <dtml-var name>.
<p>He postulated that a rose by any other name would smell just as
sweet.</p> <p>Unfortunately for you, I disagree. Access denied.</p>
</font> </dtml-if>
Now, if you view this example in your browser, you'll see an ugly Zopeerror - this is because Zope cannot locate the variable "name", despiteits best attempts to acquire it from the global namespace. Therefore, inorder to see this script work properly, you need to first make sure thata variable named "name" actually exists. You can do this by adding thisvariable to the REQUEST object, via the URL GET method; just call theDTML script using a URL similar to this one:


Here's the output:.

I wonder if you've heard of Shakespeare, Joe.

He postulated that a rose by any other name would smell just as sweet.

Unfortunately for you, I disagree. Access denied.

Flip it around to see what it looks like when the variable named "name"holds the value "neo". Here's the URL to use,


and here's the output:

Welcome to the Matrix, Neo.
In this case, since the conditional expression in the "if" statementevaluates to true, the output displayed by the script will be different.

There's one more thing that needs to be explained in the code above -the "expr" keyword. Normally, you can directly test whether or not avariable exists using the <dtml-var> or the <dtml-if> tags (as I've donein my very first example). However, if you want to do anything morecomplicated - for example, evaluate a DTML expression such as

"name == 'neo'"

"temperature > 98.6"
- you need to first tell the DTML interpreter that you're talking aboutan expression and not a variable. And that's where "expr" comes in - itlets Zope know that it's dealing with a conditional expression, and notjust a regular variable.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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