A demonstration of how macros can be used.
Macros are implemented using METAL, a language complementary to TAL, which is used to define and include macros. Macros allow parts of templates to be shared across multiple templates, enabling easier maintenance when dealing with several templates for the same site.
This example is included in the PubTal download.
This example uses a simple website with three pages:
The example uses the following configuration file:
# Use base.html as the default template <Directory> template base.html </Directory> # For the second directory, use second.html as the default template <Directory second> template second.html # Make macros in base.html available under site-macros. macro site-macros base.html </Directory>
A template which contains one or more macros can be used just like an ordinary template. The declaration of the macro doesn't interfere with its function as a template in any way. The base.html template in this example is:
<html> <body> <div metal:define-macro="navbar"> <a tal:attributes="href string:${page/depth}index.html">Home</a> <a tal:attributes="href string:${page/depth}first.html">First Page</a> <a tal:attributes="href string:${page/depth}second/index.html">Second Page</a> </div> <h1 tal:content="page/headers/title">Title</h1> <div tal:content="structure page/content">Body</div> <p metal:define-macro="email">A <b>fancy</b> email address: me@mydomain.com</p> <p>Last modified: <b tal:replace="page/lastModifiedDate">Date</b></p> </body>
To use the macros in the second template we use the metal:use-macro command. An example of a second.html template is:
<html> <body> <h1 tal:content="page/headers/title">Title</h1> <div tal:content="structure page/content">Body</div> <p metal:use-macro="macros/site-macros/email">Email goes here.</p> <p metal:use-macro="macros/site-macros/navbar">Nav bar here.</p> </body>
The second.html template can include the macros declared in the base.html template anywhere, even multiple times, using the metal:use-macro command. Any TAL that forms part of the included macro (in this case the tal:attributes commands in the "navbar" macro) will be expanded as though it had been placed directly in the second.html template.
PubTal Version 1.0