Zope
  Home arrow Zope arrow Page 3 - ZPT Basics (part 4)
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ZOPE

ZPT Basics (part 4)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2002-10-31

    Table of Contents:
  • ZPT Basics (part 4)
  • The Idiot Box
  • The Real McCoy
  • Slotting Into Place
  • Link Zone

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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">&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


     

       

    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




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway