Zope
  Home arrow Zope arrow Page 2 - ZPT Basics (part 1)
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 
 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 1)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2002-09-18

    Table of Contents:
  • ZPT Basics (part 1)
  • The Power Of Three
  • Practical Magic
  • Anatomy Lesson
  • Putting It All In Context

  • 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

    Speeding Shipping, Improving Compliance – Read the ScanCode Systems Case Study

    ZPT Basics (part 1) - The Power Of Three
    (Page 2 of 5 )

    ZPT actually consists of three different components, which interact with each other to offer developers a fair amount of power and flexibility. Here they are:

    1. TAL: First up is the Template Attribute Language, also referred to as TAL. As per the official TAL specification, TAL is "an attribute language used to create dynamic templates...it allows elements of a document to be replaced, repeated, or omitted." Put more simply, TAL is a template language which uses special attributes within HTML tags to perform different actions, like variable interpolation, tag repetition and content replacement.

    2. METAL: Nope, this isn't the music that makes your head hurt and your eardrums pound. METAL is the Macro Expansion Template Attribute Language, and it is defined as "an attribute language for structured macro preprocessing". In ZPT, macros allow developers to create a single snippet of code and reuse it extensively across the application. The advantage: a change to the macro will be immediately reflected across the templates it has been used in.

    3. TALES: Apart from having a familiar ring to it, this acronym actually stands for Template Attribute Language Expression Syntax. Officially, TALES "describes expressions that may be used to supply TAL and METAL with data." These expressions may be in the form of paths to specific Zope objects, Python code or strings, including strings containing variables to be substituted, and they form a significant component of ZPT.

    Let's see what all this means with a quick look at some code. {mospagebreak title=What's In A Name?} Consider the following simple code snippet:

    My name is <b tal:content="template/id">template id</b>.
    If you were to render this through Zope, in a template with the ID "MyFirstTemplate", you'd see the following output:

    My name is <b>MyFirstTemplate</b>.
    What happened here? Roughly translated, the line

    <b tal:content="template/id">template id</b>
    means "replace the content enclosed within the <b> element with the result obtained by evaluating the TALES expression [template/id]". When the template is rendered, this TALES expression will evaluate to the ID of the current page template, and will appear within the <b> tag.

    The TALES expression

    template/id
    is a path expression pointing to a specific Zope object - in this case, the ID of the current template - and it provides an easy and intuitive way of accessing the value of any object within Zope. It need not be restricted to objects alone either - TALES expressions are equally versatile at evaluating string data or Python code.

    TALES path expressions always begin with a variable name, and drill down hierarchically to the required object. A TALES path is traversed in a sequential manner until the final step is reached or until an error occurs. The result of the path expression is the value of the object found at the end of the path.

    The "tal:content" attribute indicates to the Zope parser that *only* the content within the enclosing element should be replaced with the results of the TALES expression. TAL comes with a number of such special attributes, each with its own particular function - I'll be discussing them in detail over the course of this tutorial.

    Note the special "tal:" namespace, which must prefix every TAL attribute, and which provides developers with an easy way to separate their own custom names from those defined in the TAL specification.

    Usage of the TAL namespace also has one very important additional benefit, closely related to the developer/designer interaction problem discussed on the previous page. Most HTML editors will not understand the TAL namespace prefix or attributes, and will therefore ignore it when rendering a page; this allows designers to view and make changes to the HTML interface of a page without interfering with its business logic or requiring the intervention of developers (so long, obviously, as they don't fiddle with the special TAL statements embedded within the code).

    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 2 hosted by Hostway