[Zope] Binding Variables
Dieter Maurer
dieter@handshake.de
Wed, 25 Jul 2001 21:16:18 +0200 (CEST)
Gustavo Vieira Goncalves Coelho Rios writes:
> May some one give me a light about binding Variables in Zope?
>
> Inside a python script, when should i use context.xxx or container.xxx ?
>
> On the zope book, page 136 there is:
>
> Context
> The Context binding defaults to the name context. This variable refers to the object that the script is called on.
>
>
> Ok! So if i call a python script inside a DTML Method, the context is the DTML method, if i call the script directly by its URL the context would be the directory, right?
No.
Acquisition is quite difficult. So difficult that even the Zope Book
Editors sometimes get it wrong (or make didactical simplifications).
When you access a Python Script, you access it through some
object, e.g. in the form
object.script
The acquisition magic makes of this
script.__of__(object)
"object" is called the "context" or "parent".
Inside the script "context" in bound to this "object".
In your example, the DTML method is not used for lookup.
The context, therefore, is not the DTML object but
the first object on the DTML namespace that actually knows
about the script.
Now to "container": The script is somewhere in the site hierarchy.
The "container" is the container that contains the script.
Unless the script is acquired, "context" and "container" are the same.
A small example -- the following site structure:
F/
script
F_sub/
method: ... <dtml-var script> ...
In this case, "context" is "F_sub" and "container" is "F".
More info on acquisition in
URL:http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html
Dieter