The example 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 ZPT. 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, while a negative result returns false.
Here's an example which illustrates how these operators can be combinedwith the "condition" attribute discussed on the previous page to set upmultiple decision branches within a template.
<span tal:define="global name string:max"></span>
<span tal:condition="python:name == 'neo'">
<font face="Arial" size="2">Welcome to the Matrix, Neo. Access
granted.</font> </span>
<span tal:condition="python:name != 'neo'">
<font face="Arial" size="2">I wonder if you've heard of Shakespeare,
<span
tal:content="name">name here</span>.
<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> </span>
Here's what the output looks like:
Welcome to the Matrix, Neo. Access granted.
Now, try changing the value of the "name" variable in the first line ofthe template,
<span tal:define="global name string:joe"></span>
and look what happens:
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.
Nothing too complicated here. A quick glance at the code will show youthat I've used the TAL "condition" attribute twice, in two differenttests. The first time around, the first conditional test will evaluateto true and the second will evaluate to false, because the value of thevariable is set to "neo"; consequently, the contents of the first <span>block will be displayed, while the contents of the second will beomitted.
<span tal:condition="python:name == 'neo'">
<font face="Arial" size="2">Welcome to the Matrix, Neo. Access granted.
</font></span>
The second time around, since the value of the variable is no longer"neo", the second test will evaluate to true and the contents of thesecond <span> block will be displayed.
<span tal:condition="python:name != 'neo'">
<font face="Arial" size="2">I wonder if you've heard of Shakespeare,
<span
tal:content="name">name here</span>.
<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> </span>
I know what you're thinking - this would have been much easier toimplement with an "if-else" conditional statement. Unfortunately, TALdoesn't have one of those yet; the only way to obtain that functionalityis to simulate it via multiple "condition" attributes, as in the exampleabove.