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

Chris Withers chris at simplistix.co.uk
Tue Jan 30 19:55:26 CET 2007


Philipp von Weitershausen wrote:
>>> from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
>>
>> This was my mistake, that should have been:
>>
>> from Products.Five.browser.pagetemplatefile import \
>>      ZopeTwoPageTemplateFile
> 
> They're the same. It shouldn't make a difference.

No, they're not...

Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

...just imports ViewPageTemplateFile from there, it's origin, if you 
read pagetemplatefile.py is:

from zope.app.pagetemplate.viewpagetemplatefile import \
     ViewPageTemplateFile

...and it appears to be a total red herring, since it's not actually 
used anywhere inside pagetemplatefile.py :-(

Maybe it could get removed?

>> However, now using the right class...
>>
>> my_macro = ZopeTwoPageTemplateFile('my_macro.pt')
>>
>> my_macro.macros just gives me a ComputedAttribute object (rather than 
>> the macros I'd expect).
>>
>> If I do, my_macro.pt_macros(), I get the macros.
>>
>> Why isn't ComputedAttribute working in this example?
> 
> Coz ComputedAttribute is a stupid Zope2ism that only works in an 
> acquisition context.

Yup, I got that after much nashing...

> I still think you're overcomplicating things. 

Indeed, here's what I've ended up with, which I think is just fine...

mymacro.pt
----------
<html metal:define-macro="template">
...
</html>

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

myview.py
---------
from macros import mymacro

class MyView(BrowserView):
    mymacro = mymacro

myview.pt:
<html metal:use-macro="view/mymacros/macros/template">
...
</html>

I find this much more lightweight, not to mention secure, than 
registering a full blown browser page just for some macros...

Chris

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


More information about the z3-five mailing list