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