[Zope] How to use ZPT with ad-hoc context?

Hedley Roos hedley at upfrontsystems.co.za
Mon May 4 05:59:21 EDT 2009

Daniel Dekany wrote:
 > How to create a template context ("here" inside ZPT) that is not an
 > object from the ZODB, just a temporary object? This is what I tried:
 > class AdhocContext(Implicit):
 >     pt = PageTemplateFile("whatever/path",  globals())
 >     ...
 > MyZopeProduct:
 >   def whatever(self):
 >         "Test"
 >         ctx = AdhocContext().__of__(self)
 >         return ctx.pt()
 > The problem I have with this is that I can't access anything in
 > AdhocContext from the ZPT because the security manager blocks it (I
 > didn't forget to security.declarePublic + document what I wanted to
 > access). Is there a simple trick to solve this? (BTW, I will need to
 > invoke some Plone macros from that ZPT too... I hope that will just
 > work if this security matter is solved.)

You don't have to create the page template as an attribute of a class. 
You can declare it as a local variable

pt = ZopeTwoPageTemplateFile('template.pt')

and then do

extra_context = {'context': some_context}
html = pt.pt_render(extra_context=extra_context)

The context variable in your template will then be what you want it to be.

If you still encounter security problems then add this line directly 
after you declare class AdhocContext

__allow_access_to_unprotected_subobjects__ = 1

You probably should not use that line too often :)


More information about the Zope mailing list