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


    Table of Contents:
  • ZPT Basics (part 4)
  • The Idiot Box
  • The Real McCoy
  • Slotting Into Place
  • Link Zone

  • 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 4) - The Idiot Box
    ( Page 2 of 5 )

    Every macro has two syntactical parts: the macro definition, and the macro invocation. Here's a simple example:

    <html> <head> </head> <body> <comment> <b metal:define-macro="author">John Doe</b> <i metal:define-macro="title">A Million Channels, And Nothing To Watch</i> </comment> <p> The book <span metal:use-macro="container/BookReview/macros/title">book title</span>, written by <span metal:use-macro="container/BookReview/macros/author">author name</span> is a stunningly original piece of literature. </p> <hr> Send mail to <span metal:use-macro="container/BookReview/macros/author">author name </span> with feedback on <span metal:use-macro="container/BookReview/macros/title">book title</span>. </body> </html>
    Here's what the output looks like:



    There are two parts to using a macro in your ZPT code: first, you define the macro, using the "define-macro" attribute, as in the code snippet below,

    <comment><b metal:define-macro="author">John Doe</b> <i metal:define-macro="title">My Story</i> </comment> // snip
    and then you use it via the "use-macro" attribute, as below:

    The book <span metal:use-macro="container/BookReview/macros/title">book title</span>, written by <span metal:use-macro="container/BookReview/macros/author">author name</span> is a stunningly original piece of literature.
    In the lines of code above, I've used a TALES path expression to access the value of the "author" and "title" macros. Note that the path expression begins with the special pre-defined "container" variable, followed by the name of the Page Template containing the macro definitions. Since the macros are exposed as attributes of the Page Template, the expression

    container/BookReview/macros/title
    returns the value stored in the macro "title".

    Take a quick peek at the HTML code that is generated:

    <html> <head> </head> <body> <comment> <b>John Doe</b> <i>A Million Channels, And Nothing To Watch</i> </comment> <p> The book <i>A Million Channels, And Nothing To Watch</i>, written by <b>John Doe</b> is a stunningly original piece of literature. </p> <hr> Send mail to <b>John Doe</b> with feedback on <i>A Million Channels, And Nothing To Watch</i>. </body> </html>
    In the original code, I have a plethora of <span> elements; however, none of them make an appearance in the rendered output. The reason for this is simple: the "use-macro" attribute replaces everything, including its containing element (which was the <span> element) with the results of the macro.

    Now, if you've been paying attention, you're probably wondering - why not just use the "define" TAL attribute for this? The next page should make things clearer.

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