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.
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.
</font>
</dtml-if>
<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:
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'"
or
"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.