[Zope] Promlem getting the current object in PythonScript

Evan Simpson evan@4-am.com
Wed, 4 Apr 2001 12:58:58 -0500


From: "Jérôme Loisel" <jerome@levinux.org>
> My basic question is this: I have code which is called from
> standard_html_footer. When that code was a DTML Method, I used this().
Now
> that this code is a PythonScript, how do I access the same object?
"context"
> and "context.this()" do not work reliably.

In this sort of situation, "context" is very different from "this()".

Short answer:

Use the Script's Bindings tab to bind the caller's namespace to "_" (or
some other name) and use _["this"] to refer to the document.

Long answer:

When your document calls standard_html_footer, it acquires it from the
document's context and passes the namespace to it.  When
standard_html_footer calls your Script, it gets it from the namespace,
which acquires it from the original document's context.  Now, the
Script's context is the context of the original document, for instance
the Folder containing that document.

The only way that the Script's context could be the document is for the
Script to be acquired from the document.  DTML Documents don't
implicitly acquire, so this can't happen.

On the other hand, if the Script can get access to the document's
namespace, it can ask the namespace for "this".  The topmost object on
the namespace stack that has "this" is the document, and calling it
returns a reference to the document.

Cheers,

Evan @ digicool & 4-am