[Zope] different namespaces in a python script by callingit by<dtml-var xxx> and <dtml-var "xxx()">?

Grégoire Weber gregoire.weber@switzerland.org
Mon, 23 Jul 2001 12:46:30 +0200


Hi Chris,

I think my explanations were a bit confusing, sorry for that!

I think you wrote down the same much more adequat and in proper english in:

   http://www.zope.org//Wikis/DevSite/Proposals/NamespacePassingRevisited
   http://www.zope.org//Wikis/DevSite/Proposals/NamespacePassingRevisited

http://www.zope.org/Wikis/DevSite/Proposals/NamespacePassingRevisitedDiscuss
ion

Sorry, I found it but now by searching for the right keyword ...

Why is the proposal not listet in

   http://www.zope.org/Wikis/DevSite/Proposals/FrontPage ?

I've also done further research (which contradict some of your comments 
in the above document). My Results (Zope 2.3.3, it seems there were
some changes since 2.2.x):

Python Script with one parameter: def xxx(anypar="foo_default"):

   <dtml-var xxx> --> ('foo_sort_on', 'foo_default')
   <dtml-var "xxx(anypar='foo_par',_=_)"> --> ('foo_sort_on', 'foo_par')
   <dtml-var "xxx(_=_)"> --> ('foo_sort_on', 'foo_default')
   <dtml-var "xxx()"> --> ('sort_order not defined', 'foo_default')
   <dtml-var "xxx(_.None,_=_)"> --> ('foo_sort_on', None)
   <dtml-var "xxx(_.None,_,anypar='foo_par')"> --> TypeError: too many
arguments; expected 1, got 2

Python Script without parameters: def xxx():

   <dtml-var zzz> --> foo_sort_on
   <dtml-var "zzz(_=_)"> --> foo_sort_on
   <dtml-var "zzz()"> --> sort_order not defined
   <dtml-var "zzz(_.None,_=_)"> --> TypeError: no arguments expected

DTML-Method:

   <dtml-var yyy> --> ('foo_sort_on', 'foo_default')
   <dtml-var "yyy(_=_)"> -->  NameError: REQUEST
   <dtml-var "yyy(anypar='foo_par',_=_)"> --> ('sort_order not defined',
'foo_par')

My conclusions
--------------

   1. <dtml-var pymethod> equals to <dtml-var "pymethod(_=_)"> 
   2. To pass the '_' namespace with other variables pass 
      it the following way: <dtml-var "pymethod(anypar, _=_)">
   3. DTML-methods: do not pass parameters in parenthesis.
      If you do you get problems with the '_' namespace.
      Best would be to pass them by pushing them on the
      namespace before by a 'dtml-let'.

Important comment!
------------------

Chris, I don't like to bore you about this. You absolutely do not have 
to answer to this e-mail if it consumes too much time to do so!


See comments to your comments below.
 
> > Questions which turn up (just for interest):
> > 
> > 1) Why isn't this done automagically by setting the 'Namespace'
> >    thing in the bindings tab to '_'? So that the caller does not
> >    have to grant access to '_' to the calles Python Script
> >    explicitly?
> 
> Why should the function being called have influence over the parameters
passed
> to it?
> By going into python (anything inside double quotes in DTML), you bypass=
 the
> (bad) magic which usually does this.

Ok, I understand. That was the design decision done by DC.

So the <dtml-var xxx> and the <dtml-var "xxx()"> calls really 
have different behaviour. I think this should be known. I mean,
I read a lot of docs over the last two years before I really begun
programming and I didn't find any pointers about that.

> > 2) Isn't there really any possibility for the Script to access
> >    the '_' namespace of it's own? It seems that I have access
> >    to the '_' namespace version which is set up at the
> >    beginning of the request.
> 
> What are you looking to achieve here?
I couldn't reproduce this 'problem'. There wasn't probably one. :-)

> > 3) Why do DTML know about the actual namespace of the calling
> >    methods and Python Scripts not?
> 
> They both know about exactly the same things. If you call a DTML method
with:
> 
> <dtml-var "my_dtml_method()">
> 
> ...you will experience exactly the same problems.
You're damn right!

> > It seems to me that Python Scripts do not at all have the same
> > behaviour as DTML methods have. :-(
> 
> Nah, they all behave the same way in this situation: badly. Viva la New
> Religion.
Dito!

> > P.S.: Is there a documentation about thsi anywhere?
> 
> ...the source :-S
> ...the mailing list archives...
> 
> Old Skool Zope Style,
He, he, yes. I've done this but I didn't find any answers about why 
it is how it is! Never mind!

Greetings, Gregoire

P.S.: The code for your convienience

-------------------------------- xxxcaller
<dtml-var standard_html_header>
<h2><dtml-var title_or_id> <dtml-var document_title></h2>

<pre>
<dtml-let sort_order="'foo_sort_on'">
<b>&lt;dtml-var xxx&gt;</b> --> <dtml-var xxx>
<b>&lt;dtml-var "xxx(anypar='foo_par',_=_)"&gt;</b> --> <dtml-var
"xxx(anypar='foo_par',_=_)">
<b>&lt;dtml-var "xxx(_=_)"&gt;</b> --> <dtml-var "xxx(_=_)">
<b>&lt;dtml-var "xxx()"&gt;</b> --> <dtml-var "xxx()">
<b>&lt;dtml-var "xxx(_.None,_=_)"&gt;</b> --> <dtml-var "xxx(_.None,_=_)">
<b>&lt;dtml-var "xxx(_.None,_,anypar='foo_par')"&gt;</b> --> TypeError: too
many arguments; expected 1, got 2
</dtml-let>
</pre>

<pre>
<dtml-let sort_order="'foo_sort_on'">
<b>&lt;dtml-var zzz&gt;</b> --> <dtml-var zzz>
<b>&lt;dtml-var "zzz(_=_)"&gt;</b> --> <dtml-var "zzz(_=_)">
<b>&lt;dtml-var "zzz()"&gt;</b> --> <dtml-var "zzz()">
<b>&lt;dtml-var "zzz(_.None,_=_)"&gt;</b> --> TypeError: no arguments expected
</dtml-let>
</pre>

<pre>
<dtml-let sort_order="'foo_sort_on'">
<b>&lt;dtml-var yyy&gt;</b> --> <dtml-var yyy>
<b>&lt;dtml-var "yyy(_=_)"&gt;</b> -->  NameError: REQUEST
<b>&lt;dtml-var "yyy(anypar='foo_par',_=_)"&gt;</b> --> <dtml-var
"yyy(anypar='foo_par',_=_)">
</dtml-let>
</pre>

<dtml-var standard_html_footer>

-------------------------------- xxx
## Script (Python) "xxx"
##bind container=container
##bind context=context
##bind namespace=_
##bind script=script
##bind subpath=traverse_subpath
##parameters=anypar='foo_default'
##title=
##
if _.has_key('sort_order'): return (_['sort_order'],anypar)
else: return ('sort_order not defined',anypar)

-------------------------------- yyy
<dtml-unless anypar><dtml-call "REQUEST.set('anypar',
'foo_default')"></dtml-unless>
<dtml-if sort_order>('<dtml-var sort_order>', '<dtml-var anypar
>')<dtml-else>('sort_order not defined', '<dtml-var anypar>')</dtml-if>

-------------------------------- zzz
## Script (Python) "zzz"
##bind container=container
##bind context=context
##bind namespace=_
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
if _.has_key('sort_order'): return _['sort_order']
else: return 'sort_order not defined'

_____________________________________
Grégoire Weber
mailto:gregoire.weber@switzerland.org