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