Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
ZPT Basics (part 4)
Get up close and personal with METAL, the ZPT macro language.

| The Idiot Box |

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:

Output image

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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
<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.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback