DTML Basics (part 2) - Comparing Apples And Oranges (
Page 3 of 8 )
The
example you've just seen was very rudimentary. To really add some
punch, you need to know how to construct what the geeks call a
conditional statement. And the very basis of a conditional statement is
comparison - for example, "if this is equal to that, do thus and such".
Since Zope is built around Python, and Python comes with a bunch of
useful operators designed specifically for use in conditional
statements, you can use Python operators to build conditional statements
in your DTML. Here's a
list:
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. A
positive result returns true (1), while a negative result returns false
(0).
Here's an example, a DTML Method named "access", which illustrates how
these 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 Zope
error - this is because Zope cannot locate the variable "name", despite
its best attempts to acquire it from the global namespace. Therefore, in
order to see this script work properly, you need to first make sure that
a variable named "name" actually exists. You can do this by adding this
variable to the REQUEST object, via the URL GET method; just call the
DTML script using a URL similar to this one:
http://localhost:8080/DTML%20Basics/access?name=Joe
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,
http://localhost:8080/DTML%20Basics/access?name=neo
and here's the output:
Welcome to the Matrix, Neo.
In this case, since the conditional expression in the "if" statement
evaluates 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 a
variable exists using the <dtml-var> or the <dtml-if> tags (as I've done
in my very first example). However, if you want to do anything more
complicated - 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 about
an expression and not a variable. And that's where "expr" comes in - it
lets Zope know that it's dealing with a conditional expression, and not
just a regular variable.