[Zope] dtml-if syntax and comparing variables

Martijn Pieters mj@antraciet.nl
Mon, 08 Nov 1999 10:09:23 +0100


At 08:53 08/11/99 , Roché Compaan wrote:
> > When I answer questions on the list, I tend to answer with a specific
> > solution. In some instances, this is just what the person
> > wants... at other
> > times, the person is really looking for more general guidance
> > about finding
> > that kind of solution. It's hard to tell sometimes which way to answer :)
>
>Thanks for wonderful guidance.  I had no problem with your answer.  My
>frustration was with dtml in particular.  What particularly confused me in
>my initial effort was that i could render the values of two variables with
>the same values but when i compared these variables in an expression nothing
>happend eg:
>   <dtml-with objectValues>
>      <dtml-var URL>   // renders a URL  mysite/folder
>      <dtml-var absolute_url>  //render absolute_url  mysite/folder
>      <dtml-if "URL==absolute_url">   // even if the values where the same it
>always did the else part
>         do something
>      <dtml-else>
>         do something else
>      </dtml-if>
>   </dtml-with>

This stems from the fundamental difference between <dtml-val absolute_url> 
(not quoted) and <dtml-var "absolute_url"> (quoted). As Kevin pointed out, 
the first is the equivalent of <dtml-var name="absolute_url">, while the 
second is the same as <dtml-var expr="absolute_url">.

If you reference an object or method or a variable by it's name (non-quoted 
or name attribute), Zope will figure out if it is a callable method, and 
will call it for you if it is. It will also provide a context for DTML 
Methods and Documents when calling them.

Inside a Python reference, you will have to do this figuring out yourself, 
a callable object or method will not be called automatically anymore. With 
a method like absolute_url this is easy: <dtml-var "absolute_url()"> now 
returns the same as <dtml-var absolute_url>.

With a DTML Method or Document you will have to pass in some context 
however. You do this by passing in a 'client' (_.None wil do here, don't 
ask), and a namespace, which is _:

   <dtml-var "MyDTMLMethod(_.None, _, anyotherarguments=gohere)

If you want to replicate the name="" behaviour in your Python expressions, 
you can use do a namespace lookup like <dtml-var "_['absolute_url']">. The 
getitem method will do the same, only now you can specify wether or not you 
want the object to be called if it is callable: <dtml-var 
"_.getitem('absolute_url', 0)"> will return the method itself, not it's 
result. Make the 0 a 1, and it is called again.

Hope this clears things up a bit.

--
Martijn Pieters, Web Developer
| Antraciet http://www.antraciet.nl
| Tel: +31-35-7502100 Fax: +31-35-7502111
| mailto:mj@antraciet.nl http://www.antraciet.nl/~mj
| PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149
------------------------------------------