ZPT Basics (part 4) - Slotting Into Place (
Page 4 of 5 )
METAL also introduces the concept of
"slots" within a macro, which may in turn be populated by other macros. Slots
are placeholders within a macro definition, which can be populated dynamically
every time you use the macro.
Let's take a simple example to demonstrate how this works. Here's the macro definition,
which contains a placeholder, or slot, named "link":
<p metal:define-macro="spotlight">
Today's special is <span metal:define-slot="title"></span>
</p>
And here's the template which uses it - note how the placeholder has been filled
with an actual value using the "fill-slot" attribute:
<p metal:use-macro="container/macroDef/macros/spotlight">
Today's special
is <span metal:fill-slot="title"><a
href="http://www.melonfire.com/community/columns/trog/">the
DTML Basics
series</a></span> </p>
Here's what the output looks like:
By using a slot within a macro, I have the option of reusing the macro multiple
times, and replacing the slot with different data each time I use it. This adds
a tremendous amount of reusability to the macro, and can thereby substantially
simplify the task of developing almost-but-not-quite-identical programming routines.
Here's another, more realistic example - a macro which sets up the basic skeleton
for a Web page, and uses slots to populate each instance of that page with different
content:
<span metal:define-macro="body">
<table width="100%" cellspacing="10"
cellpadding="10">
<tr>
<td colspan="2">Logo</td>
</tr>
<tr>
<td><span
metal:define-slot="menu">Menu</span></td>
<td><span metal:define-slot="content">Content</span></td>
</tr>
<tr>
<td
colspan="2"><hr>All rights reserved.</td>
</tr>
</table>
</span>
Here's a sample Web page that uses the macro, and defines a particular look for
the main body:
<html>
<body>
<span metal:use-macro="container/macroDefs/macros/body">
<span
metal:fill-slot="menu">
Select from the list below:
<br>
Item A
<br>
Item
B
<br>
Item C
<br>
</span>
<span metal:fill-slot="content">
<center>
Content
goes here <p>Content goes here <p>Content goes here <p>Content
goes here
<p> </center> </span> </span>
</body>
</html>
Here's the output:
And here's another one that uses the same macro, but a different look for the
body:
<html>
<body>
<span metal:use-macro="container/macroDefs/macros/body">
<span metal:fill-slot="menu">
</span>
<span metal:fill-slot="content">
<table
width=100% cellspacing="5" cellpadding="5">
<tr><td>Item A</td><td>Item
B</td><td>Item C</td></tr>
<tr><td colspan="3"><hr></td></tr>
<tr><td
colspan="3" align="center">Content goes here<p>Content goes
here</p><p>Content
goes here</p><p>Content goes here</p><p>Content goes
here</p><p></p></td></tr>
<table> </table></table></span> </span>
</body>
</html>
Here's the output:
As you can see, the same macro's been used in both examples; however, merely
by changing the contents of the slots within the macro, I can generate a completely
different look for each Web page.