[ZPT] Acquiring PT local context (e.g. state of defines, repeat) within python scripts?
Jeffrey P Shell
jeffrey@cuemedia.com
Fri, 27 Dec 2002 15:15:45 -0700
I'd rather keep the argument lists long and specific than have
unexpected consequences as the result of namespace pollution. For one
thing, the ``context.mystring`` bit could become confusing to another
maintainer that's trying to determine where ``mystring`` is defined.
This was a big problem with DTML - the "try to guess where THIS name
came from!" game could lead to many hours of frustration.
If your argument lists are getting too long and complex to manage, it
could be considered a "bad smell" in refactoring terms. It might be
worthwhile to examine your code situation and evaluate different ways
of doing it. For some of my really complex pages, I use some
pre-loader scripts like ``getExtendedDataFor(...)``, which usually
return a mapping full of complex data. I use them at the beginning of
a page template (before calling any macros even) with something like::
<tal:pre define="global extended
python:here.getExtendedDataForFoo(...)" />
I've had decent success with this setup.
"Explicit is better than implicit"
On Thursday, December 26, 2002, at 01:30 PM, Jeff Kowalczyk wrote:
> I'm making every effort to keep logic out of the Page Templates I'm
> writing, creating many
> python scripts in the process. The parameter lists to some of the
> scripts called with
> python:here.myScript() get pretty long and complex.
>
> I'd like to try acquisition in the python scripts to get at the Page
> Template execution
> 'context', such as the current tal:defined variables, the current
> state of the
> batch/repeats, etc., without making an enormous parameter lists.
> Something like the
> following pseudocode (which fails with an attribute error, obviously):
> <html><body>
> <p tal:define="mystring string:yada">This is the result of the
> call to script1.<br>
> <pre tal:content="here/script1">Script Results</pre></p>
> </body></html>
>
> request = container.REQUEST
> RESPONSE = request.RESPONSE
> print 'context.mystring:', context.mystring
> return printed
>
> I think it could be useful and productive if I coded fairly
> defensively in the python
> scripts to gaurd against acquisition misses.
It's not acquisition, it's games-with-namespaces. If you spend all
your time coding fairly defensively against potential issues with
implicit object passing, you may as well just use explicit object
passing and *KNOW* what you're going to get, rather than hope.
> Can I bind to something in a python script that gives me access to the
> current PT
> environment as described?
> If it is a common technique, would anyone have a link to somewhere I
> could read up on this
> specific technique of using scripts with page templates? Thanks.