Zope
  Home arrow Zope arrow Page 6 - ZPT Basics (part 2)
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ZOPE

ZPT Basics (part 2)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2002-09-25

    Table of Contents:
  • ZPT Basics (part 2)
  • A Little Male Bonding
  • The Number Game
  • A Question Of Scope
  • Switching Things Around
  • Comparing Apples and Oranges
  • If It's Thursday, It Must be Italy
  • Submitting To The King
  • Looking Ahead

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    ZPT Basics (part 2) - Comparing Apples and Oranges
    (Page 6 of 9 )

    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


     

       

    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




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway