Using Zope Page Templates macros in other templates

I won't try to discuss here the benefits of ZPT macros vs. viewlets and content providers or viceversa or when to use each of them, I'll just show a simple (basic even) technique of reusing template code across page templates, using the macros mechanism.

Trying to get the macros with something like context/@@page_with_macros/macros/the_macro doesn't work, as BrowserView (and BrowserPage) doesn't know anything about Zope Page Templates. Of course, that's also because Zope 3 doesn't directly publishes templates, only pages. The technique involves setting the page template who's macros you want to access as attributes of the curent page, something along these lines:

class MyPage(BrowserPage):rows
     macro_template = ViewPageTemplate('template_with_macros.pt)

In the template associated with this page, you can have:

<metal:macro metal:use-macro="view/macro_template/macros/the_macro">


Comments