[Zope] external methods

Amos Latteier amos@aracnet.com
Tue, 30 Mar 1999 09:28:35 -0800


At 10:12 AM 3/30/99 +0400, Service Informatique CHSR wrote:
>>Nope,
>>
>>With that definition, my_method expect to be called with the arguments
>>set, eg <!--#var "my_method('Zope')"--> to display "Zope is cool".
>>
>>However, I want to get the variable l from the namespace. (application
>>variables made with REQUEST.set, Folder or document properties, etc.)
>>The question was: is self.REQUEST the only way to access the
>>variable namespace? And the only way to *modify* the variable namespace
>>from external methods (with self.REQUEST.set)?

Well, this is an interesting question. In my experience, seldom do people
try to munge the DTML namespace from within an External Method. I guess it
could be done (perhaps by passing the special underscore variable to an
External Method), but I wonder if this is really the best way to solve your
problem.

>I'd like to be more precise. Suppose my_method is:
>
>def my_method(self):
>    if self.REQUEST.has_key('my_var'):
>        return "oh yes"
>    return "boooo"
>
>
>Now, I write in DTML:
>
><p><!--#var my_method--></p>
><p><!--#with "_.namespace(my_var='my_value')"-->
><!--#var my_method--><br>
><!--#var my_var-->
><!--#/with--></p>
>
>Believe me or not, the result is:
>
>boooo
>boooo
>my_value
>
>You can now understand why I ask how to access the variable
>namespace from external methods. How can I test the existence
>of my_var, and access its value???

Well let's see what's going on here. You call my method, it acquires the
REQUEST object and looks for a 'my_var', doesn't see it and returns
'boooo'. so far so good. Now you push a namespace containing only 'my_var'
on to the DTML namespace stack and call the method again. Not surprisingly
the method does the same thing as before, it acquires the REQUEST and finds
that it does not contain 'my_var' so it prints 'boooo'. Finally you call
'my_var' which is found in the DTML namespace and 'my_value' is printed.

So the lesson is: the DTML namespace is not the same thing as the REQUEST
object.

For more on how DTML namespace works see my advanced DTML how-to:

http://www.zope.org/Documentation/HowTo/DTML

My advice to you is to keep things simple. It's easy to confuse yourself
with the complexities of DTML. In general, I would suggest that your
External Methods take well defined arguments and return results, rather
than modify the objects passed to them. Then allow the DTML to pass those
arguments and munge its own namespace in response to the results of the
External Method. Of course, this schema isn't always the right one. Finally
I would suggest that you, don't munge the DTML namespace from within an
External Method unless you have a good reason to, I think that this would
make the DTML even harder to understand.

Good luck.

-Amos