[Zope] Is Zope syntax inconsistent ?
Tres Seaver
tseaver@palladion.com
Fri, 10 Mar 2000 22:27:33 -0600
Glyph Lefkowitz <glyph@twistedmatrix.com>
>
> On Fri, 10 Mar 2000, Phil Harris wrote:
>
> > As someone earlier today on the list said, "if it's in quotes it's python"
> > (or something similar).
> >
> > Basically Python rules apply when using any expressions in zope dtml-var
> > and other tags.
>
> I still think it's an inconsistent syntax. It should be possible (by
> overriding __coerce__, I think? I haven't had time to look this up yet)
> to make the == operator do something different. AUTHENTICATED_USER is
> often used as a string (at least what I've seen of it ...)
When in doubt, read the fine manual. The _actual_ syntax you are using is::
<dtml-var name="AUTHENTICATED_USER">
which can be abbreviated (and is, by almost every Zopista) as:
<dtml-var AUTHENTICATED_USER>
The "name" form of the var tag is the "preferred" one, for the types of
applications DTML is really designed for: simple scripting of objects built
in Python. It does several "automagic" things with the name:
- If it is callable, calls it, passing the current object, the
current namespace, and the REQUEST; it then replaces the object
with the result of the call.
- If it has a string coercion (__str__) method, calls that and
replaces the object with the result.
This behavior is the Right Thing (TM) for simple scripting; but it _isn't_
Pythonic (the automagic stuff smells more Perlish, really).
The "in quotes" version is really::
<dtml-var expr="AUTHENTICATED_USER">
but can also be spelt (and mostly is)::
<dtml-var "AUTHENTICATED_USER">
The "expr" form of the var tag evaluates the Python expression (in this case,
to the object AUTHENTICATED_USER). For fairly obvious security reasons, the
evaluation takes place inside a restricted namespace, without the usual
builtins; the "safe" builtins are made available as members of the
"_" namespace.
>
> Also, "if it's in quotes it's in python", why do I need to do _.str()?
> why does dir() not work? It seems like some pretty horribly munged sort
> of python...
>
> > > I would expect an object to behave consistently wherever it was used - so
> > > if it renders/returns/evaluates to 'zope' inside a dtml-var I would expect
> > > it equally to render/return/evaluate to 'zope' inside a dtml-if.
>
> > > If it doesn't then surely we have an inconsistent syntax. How do we know
> > > how any object is going to behave inside a dtml tag ?
The simplest disambiguation possible is to cease using the shortcut forms, and
always spell what you really want:
<dtml-var name="AUTHENTICATED_USER"> <!-- DWIM: give me text!!! -->
or
<dtml-var expr="AUTHENTICATED_USER.getUserName()"> <!-- DWIS,NWYTIM -->
Hope this helps!
Tres.
--
=========================================================
Tres Seaver tseaver@palladion.com 713-523-6582
Palladion Software http://www.palladion.com