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

 
Developer Updates  
Free Website Content 
 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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


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

    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 ZPT. 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, while a negative result returns false.

    Here's an example which illustrates how these operators can be combined with the "condition" attribute discussed on the previous page to set up multiple 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 of the 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 you that I've used the TAL "condition" attribute twice, in two different tests. The first time around, the first conditional test will evaluate to true and the second will evaluate to false, because the value of the variable is set to "neo"; consequently, the contents of the first <span> block will be displayed, while the contents of the second will be omitted.

    <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 the second <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 to implement with an "if-else" conditional statement. Unfortunately, TAL doesn't have one of those yet; the only way to obtain that functionality is to simulate it via multiple "condition" attributes, as in the example above.

     
     
    >>> 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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT