Home arrow Zope arrow Page 4 - ZPT Basics (part 4)

Slotting Into Place - Zope

In this concluding article, get up close and personal with METAL, the macro language that allows you to add reusability to your Zope templates.

  1. ZPT Basics (part 4)
  2. The Idiot Box
  3. The Real McCoy
  4. Slotting Into Place
  5. Link Zone
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 10
October 31, 2002

print this article


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"> &nbsp; </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.

>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates


Dev Shed Tutorial Topics: