[ZPT] Acquiring PT local context (e.g. state of defines, repeat) within python scripts?

Troy Farrell troy@entheossoft.com
Thu, 26 Dec 2002 22:03:14 -0600


I'm glad you've brought this out.  I've been wrestling with this as 
well.  It would be nice if we could call python scripts in the same 
"context."  This certainly would make our python scripts more reusable 
and flexible, and I wouldn't be using nearly so many tal:define statements.

I'm with Jeff in asking for hints.

Troy

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.
> 
> 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.