Home arrow Zope arrow Page 2 - ZPT Basics (part 4)

The Idiot Box - Zope

In this concluding article, get up close and personal with METAL, the macro language that allows you to add reusability to your Zope templates.

TABLE OF CONTENTS:
  1. ZPT Basics (part 4)
  2. The Idiot Box
  3. The Real McCoy
  4. Slotting Into Place
  5. Link Zone
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 10
October 31, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: