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

The Real McCoy - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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">&copy;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">&copy;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">&copy;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">&copy;2002, Melonfire. All rights reserved.</font> </p></div> </body> </html>


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

ZOPE ARTICLES

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