[ZPT] preload ZPT namespace

Casey Duncan casey at zope.com
Mon Jan 26 13:44:57 EST 2004


On Mon, 26 Jan 2004 11:17:21 -0600
alan runyan <runyaga at runyaga.com> wrote:

> Hi.
> 
> We are doing quite a bit of defines in a pagetemplate
> and I was wondering if you could sort of merge a
> dictionary into the ZPT execution namespace.  I know
> this is risky but I was just wondering if its possible.

Usually this is done by having a script or method do some calculations
and then call the ZPT passing it the results as args. The application
would never envoke the page template directly, it would only do so
through the method. AFAIK this is a common idiom in Zope 3. The args
passed to the ZPT are exposed through the "options" variable. Zope 3's
view machinery make this much more natural than Zope 2's skins however.
 
> We have a method that returns back a dictionary.
> I would like to load this into the global namespace for
> ZPT.  You could put this into the request - but I think
> its dirty.  I would like it only in the zpt namespace.  Also
> another question is.. if I want to access zpt namespace
> or pass it into a script.  Is that possible?  I would most
> likely *never* mutate it, just reference variables that
> have been assigned.

Either have the method call the ZPT (or create a script that calls the
method and passes the results as kwargs to the ZPT).

The ZPT namespace is like a local scope. Perhaps something like Python's
locals() built-in for ZPTs would be useful for passing the local defines
outside of the template. I don't think I've ever needed to do that in
Python before though, so it makes me think it might not be such a good
idea. I have passed globals() around, but usually to satisfy a Zope API
call.
 
> Is this reasonable?
> 
> One of things we often find in PythonScript is that we
> are constantly looking up tools where they are already
> referenced in ZPT.  This is unnecessary.  If we could
> pass in the namespace that would work and probably
> cut out lots of unnecessary traversal in Plone.

If you do it the other way: envoke the script/method first and then
envoke the ZPT, then the script can do the lookups and pass them to the
ZPT if they are costly. I would be careful optimizing this though, make
sure profiling backs up the assumption that the lookups need
optimization. They will tend to obfuscate your code.
 
> If the request is the best place to do this; thats fine.
> Just want to know if this is possible.

The request will work, but is probably too global. I have used it on
occasion as a cache storage (for example for getFilteredActionsFor,
which is arbitrarily expensive). But that caching is by design global to
the request. Putting things that are not global to the request into the
request is usually asking for trouble (but I think we've all done it).
Just make sure you comment liberally ;^)

-Casey



More information about the ZPT mailing list