[ZPT] global variables and ZPT

Evan Simpson evan@zope.com
Fri, 02 Nov 2001 11:47:40 -0500


Schmidt, Amy wrote:

> I have a main page template that contains references to other page
> templates. So my main page template is basically a container page and the
> referenced page templates are child pages. I need to set a variable in the
> container page that can be accessed by its children.


How are the "children" referenced?  Like tal:replace="structure here/child"?
Or like metal:use-macro="here/child/macros/block"?

When you use a macro, the macro body automatically sees all variables 
defined in the enclosing scope.  When you embed a page template as though it 
were content, you need to explicitly pass variables that you want it to 
have, either like this:

PT1: <div tal:replace="structure python:here.PT2(foo=foo, bar=bar)"></div>
PT2: <span tal:replace="options/foo">Foo</span>

...or like this:

PT1: <div tal:define="x python:request.set('foo', foo), request.set('bar', 
bar)" tal:replace="structure here/PT2"></div>
PT2: <span tal:replace="request/foo">Foo</span>

In the first case, you pass the values as keyword arguments, and in the 
second you stuff them into the request.

I suspect that macros might make your like easier, since their entire 
purpose is enabling shared presentation elements.

Cheers,

Evan @ Zope