[Zope] Feeling like a newbie.. document id if statement?

Rik Hoekstra rik.hoekstra@inghist.nl
Wed, 24 May 2000 16:58:29 +0200

> >  <dtml-if "_['id']=='index_html'">
> I wonder if someone would be kind enough to translate Rik's code into Enlish
> for me. I know that expressions inside "" are Python, but what is the
> function of the [] and _?

Hm, I'm afraid this is a DTML trick (nothing more). I hope I can explain
this right. Please correct me if I go wrong.

Normally in DTML, you access variables in the current namespace with
<dtml-var id>, which actually stands for <dtml-var name="id">

In an expression like the one used by J you actually use an expression,
<dtml-var expr="id">, in which case you do not get the id variable in
the current namespace, but the actual value of the id attribute
(property) of the object in the current namespace.

The problem here is that the id of index_html is a python method (which
is actually returned by <dtml-var expr="id">). This will never be equal
to 'index_html' as that is a string.

The solution is to call the variable in the current object namespace by
using it like a  dictionary in the python expression <dtml-var

Because it's a method, you could (in this case) also use <dtml-var
expr="id()">, in which case a string is returned (so that <dtml-if
expr="id()=='index_html'"> will once again be useful. However, the
_['id'] is preferred, because it will work on different objects and the
id of most other objects is a string. For example:

<dtml-in "objectValues(['DTML Method', 'File'])">
   <dtml-if "_['id'] == 'somestring'">
     do something nice

would work, whereas  <dtml-if "id() == 'somestring'"> would break if
there are objects whose ids are not methods (assuming File is one of
them, which I wouldn't know)

So the different forms are (actually there are some more, but they do
not matter here):

<dtml-var id>
<dtml-var "id">
<dtml-var "_['id']">
<dtml-var "id()">

<dtml-var "_[id]"> will give you an error

Now that was probably more English than you'd hoped for