[z3-five] (re)using zpt macros from view page templates

Chris Withers chris at simplistix.co.uk
Sun Jan 28 14:30:37 CET 2007


Andreas Jung wrote:
> 
>> I have a page template that's being used for Five view. I now want to
>> re-use part of that template as a zpt macro in another Five view.
>>
>> How should I go about doing this?
> 
> By registering the template with the macros as views?

Is that the canonical way? it seems pretty heavyweight in terms of 
instantiating a whole view, with request and all, just to get a macro...

How about:

my_macro.pt
-----------
<html metal:define-macro="template">
...etc...
<metal:x define-slot="foo"/>
...etc...
</html>

macros.py
---------
from Products.Five.browser.pagetemplatefile import \
     ZopeTwoPageTemplateFile
my_macro = ZopeTwoPageTemplateFile('macros.pt')

myview.py
---------

from macros import my_macro

class MyView(BrowserView):

     def getMacro(self):
         return my_macro.macros['template']

myview.pt
---------
<html metal:use-macro="view/getMacro">
<div fill-slot="foo">
blah
</div>
</html>

Would the above work?
Is there anything wrong with it?

cheers,

Chris

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk


More information about the z3-five mailing list