ZPT Basics (part 4) - The Real McCoy (
Page 3 of 5 )
With Web sites and applications growing ever more complex,
and with the line between interface design and business logic growing more and
more blurred every day, it's becoming harder and harder to write easily-maintainable
code. Templates can make things easier by allowing developers to separate program
code from interface elements cleanly and efficiently - and METAL's macros add
one more level of flexibility to the toolkit, making it possible to create snippets
of code that can be easily edited, referenced and yes, maintained.
Enough of the marketing talk - here's a simple example. Let's define two macros
in a template named "macroDef":
<div align="center" metal:define-macro="header">
<h3>You are currently
viewing the official Melonfire Web site. Accept no
substitutes.</h3> </div>
<p
metal:define-macro="footer">
<hr align="center">
<font size="1">©2002,
Melonfire. All rights reserved.</font> </p>
Here, the "header" and "footer" macros contain code for the display of the page
header and footer respectively. In my earlier example, my next step was to invoke
these macros from further down in the same page - but this time around, I'm going
to add a little twist (and simultaneously answer the question I posed on the previous
page) by invoking these from a different template.
Here's my second template, named "HomePage":
<html>
<body>
<div align="center"
metal:use-macro="container/macroDef/macros/header">
<h3>You
are currently viewing the official Melonfire Web site. Accept no
substitutes.</h3>
</div>
<p align="center">Page content here.</p>
<div align="center"><p
metal:use-macro="container/macroDef/macros/footer">
<hr
align="center">
<font size="1">©2002, Melonfire. All rights reserved.</font>
</p></div>
</body> </html>
Here's what the output looks like:
In this example, the "use-macro" attribute invokes the "header" and "footer"
macros at appropriate places in my page template. Since both the target template
and the macro definitions are located in the same folder, the macros can be invoked
using the same TALES expression syntax as in the previous example.
By allowing developers to define macros in one template, and use them in another
(or in a number of different templates), METAL offers far more power than the
regular TAL "define" or "replace" attributes, especially from the maintenance
point of view; a change to the macro definition is immediately reflected in all
templates that use the macro.
It isn't always necessary to keep both the macro definitions and the templates
that invoke them in the same folder - the following example demonstrates how a
macro stored in a sub-folder can be accessed by a template in the parent folder:
<html>
<body>
<div align="center"
metal:use-macro="container/Library/macroDef/macros/header">
<h3>You
are currently viewing the official Melonfire Web site. Accept no
substitutes.</h3>
</div>
<p align="center">Page content here.</p>
<div align="center"><p
metal:use-macro="container/Library/macroDef/macros/footer">
<hr
align="center">
<font size="1">©2002, Melonfire. All rights reserved.</font>
</p></div>
</body> </html>
Want to place the macro definitions in the root folder? Use the special "root"
variable in your path expression, as below:
<html>
<body>
<div align="center"
metal:use-macro="root/macroDef/macros/header">
<h3>You
are currently viewing the official Melonfire Web site. Accept no
substitutes.</h3>
</div>
<p align="center">Page content here.</p>
<div align="center"><p
metal:use-macro="root/macroDef/macros/footer">
<hr align="center">
<font
size="1">©2002, Melonfire. All rights reserved.</font>
</p></div>
</body> </html>