[Zope] how to use the document object in a script

Dylan Reinhardt zope@dylanreinhardt.com
13 May 2003 10:46:02 -0700


On Tue, 2003-05-13 at 08:08, Wim Bekker wrote:
> Wrestling with the ZopeBook (ScriptingZope.stx), I'm trying to call a script
> from a DTML document to return eq the title for the document (as learning
> curve).
[...]
> The 'myScript' has:
> Return context.title
> 
> The result is the title for the folder where the document is in. I'm
> expecting the title for the document itself. 

You've got two problems.  The first is that some objects acquire certain
attributes from their container.  A DTML Method will always return the
names defined by its container before exposing its own names.

The more fundamental problem, though, is that you aren't calling your
form object when you think you are.

Say you have two objects:

my_form (DTML Document)
my_target (Python Script)

When a client makes a request for my_form, they are *calling* the
my_form object.  Since this object returns HTML when called, they get a
nice-looking web form that includes a tag like:

<form method=post action=my_target>

>From this point on, you are **not** calling my_form any more.  Any
calculations that must be done by the my_form object must already be
done by this point.

Submitting this form calls my_target.  Any calculations performed by
my_target will be done in the context of how my_target is set up and
what request you call it with.  The my_form object has no direct
relevance at this point.

If you need for my_target to know the title property of the my_form
object that created the form, you'll want to include something like this
in my_form:

<input type=hidden value=<dtml-var title>>

This way, you're evaluating title in the context of the correct object
and passing that value along to the object you actually want to do
something with it.

Of course, DTML Methods don't have their own namespcaes so that won't
work in your case.  You can work around it (or cut & paste your code
into a DTML Document object), but it may be just as easy to hand code
it.

HTH,

Dylan