Zope
  Home arrow Zope arrow Page 2 - ZPT Basics (part 3)
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 3)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2002-10-09


    Table of Contents:
  • ZPT Basics (part 3)
  • When In Rome...
  • Members Only
  • Sticks And Stones
  • Handling Errors
  • Endgame

  • 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 3) - When In Rome...
    ( Page 2 of 6 )

    Not impressed yet? Let's alter the code a little to see what else you can do with sequences.


    <span tal:repeat="fruits python:'apple', 'banana', 'orange', 'apricot', 'grape'"> <i tal:condition="repeat/fruits/start">Here's where it all starts</i> <br> I am a <span tal:replace="fruits" />, my sequence position is <span tal:replace="repeat/fruits/number" />, and my index number is <span tal:replace="repeat/fruits/index" />. <br> <i tal:condition="repeat/fruits/end">And here's where it all ends.</i> </span>
    Now take a look at the output.

    Here's where it all starts I am a apple, my sequence position is 1, and my index number is 0. I am a banana, my sequence position is 2, and my index number is 1. I am a orange, my sequence position is 3, and my index number is 2. I am a apricot, my sequence position is 4, and my index number is 3. I am a grape, my sequence position is 5, and my index number is 4. And here's where it all ends.
    How does this work? Well, you may remember, from your DTML exploits, that all sequences have built-in variables that can be used to identify the current pointer position within the sequence. The "number" variable represents the position of the current item in the sequence, while the "index" variable provides an alternative zero-based indexing mechanism that serves the same purpose. And the "start" and "end" variables provide an easy way to find out if you're at the beginning or end of the sequence - the former is true when the current item is the first element of the sequence and the latter is true when the current element is the last element of the sequence.

    Now, in order to access these values in your Zope Page Templates, you need to use one or more TALES expressions. Here are the ones I've used:

    repeat/fruits/start - A TALES expression for the value of the "start" variable

    repeat/fruits/number - A TALES expression for the value of the "number" variable

    repeat/fruits/index - A TALES expression for the value of the "index" variable

    repeat/fruits/end - A TALES expression for the value of the "end" variable

    You may remember, from earlier articles in this series, that every TALES path must begin with a variable name. "repeat" is one such pre-defined variable, and it stores information on the start and end position, number, index and length of the sequence. This information can then be accessed using a three-part path, such as the ones demonstrated above

    The example you just saw used a simple sequence. However, you can also loop over a collection of objects using the same technique. Take a look at a simple example that lists the objects in the current folder.

    This folder contains: <ul> <li tal:repeat="filelist container/objectIds"> <span tal:replace="filelist"/> </li> </ul>
    Here's the output:

    This folder contains: * CountingDown
    Here, I've used the "container" built-in variable together with the "objectIds" variable, which returns a sequence with the list of objects in the current folder.

     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT