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,
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.