Home arrow Zope arrow Page 6 - ZPT Basics (part 2)

Comparing Apples and Oranges - Zope

Dig deeper into the world of ZPT with examples andillustrations of how to create, manipulate and use variables andconditional tests in your templates.

TABLE OF CONTENTS:
  1. ZPT Basics (part 2)
  2. A Little Male Bonding
  3. The Number Game
  4. A Question Of Scope
  5. Switching Things Around
  6. Comparing Apples and Oranges
  7. If It's Thursday, It Must be Italy
  8. Submitting To The King
  9. Looking Ahead
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 13
September 25, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

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

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