[Zope] dtml-if syntax and comparing variables

Kevin Dangoor kid@kendermedia.com
Sun, 7 Nov 1999 12:31:49 -0500


----- Original Message -----
From: Roché Compaan <roche@up-front.co.za>
To: Zope <zope@zope.org>
Sent: Sunday, November 07, 1999 11:00 AM
Subject: RE: [Zope] dtml-if syntax and comparing variables


> But the solution to the problem really makes me sigh deeply.  I currently
> have more solutions than understanding.  The Zope Quick Reference quickly
> mentions "this" as "a handy way to talk to ourselves in document
templates".
> I am familiar with "self" in many programming languages and "self" would
> have enlightened "this" issue a bit.

"self" is a pretty common construct, that's true. I think I've seen this()
in a few languages as well...

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 :)

> 1.What are the rules for using quotes in dtml?

<dtml-var foo> is equivalent to <dtml-var name="foo">
<dtml-var "foo"> is equivalent to <dtml-var expr="foo">

So, what does this mean to you? If you are using <dtml-var> and you put the
argument in quotes, you are actually specifying an expression. This
expression needs to follow the basic structure of an expression in Python.
(That's one place where knowing python will help you). So, you'll generally
put a pythonish expression in quotes.

<dtml-if "this() == PARENTS[0]"> essentially evalutes the this() ==
PARENTS[0] part as a python expression. Within the context of a <dtml-in>,
this() will return the object that you have iterated to. PARENTS[0] is
equivalent to self. You *could* do this:

at the beginning of the method, put
<dtml-call "REQUEST.set('self', this())">

Then, you could do:
<dtml-if "this() == self">

which may seem clearer. But, the PARENTS[0] thing will work just dandy.

> 2.The nitty gritty of passing, comparing and setting variables in less
than
> 6 hours.

DTML is an environment that is restricted for the sake of security... that
may make it seem difficult to set variables, etc. But it is not really that
bad.

Comparing variables is pretty easy:

<dtml-if "foo == bar">

will do a comparison of foo and bar, looking them both up in the namespace.
Those variables could have been set by you, could have been passed in via a
form, etc.

To set a variable, use <dtml-call "REQUEST.set('variable', value)">. Note
that this is in quotes, so you need to follow the python rules of
expressions. You can put strings in single quotes, numbers don't need any
quoting. You can even make a list, <dtml-call "REQUEST.set('mylist', ['foo',
'bar'])">

What if you want to change the variable? You are not allowed to do "foo = 2"
in DTML. Let's say that you set a variable called "speed" to 55, and you
want to add 20 to it. You would have to do <dtml-call "REQUEST.set('speed',
speed + 20)">. If you have a variable that is a list, you can append to it
like so: <dtml-call "mylist.append('nextelem')">

You can also set variables temporarily using the <dtml-let> tag. These
variables only exist between the <dtml-let> and </dtml-let>

To pass variables to another DTML Method,

<dtml-call "mymethod(_.None, _, var1=5, var2='hello')">

The first two parameters pass along the namespace. After that, you can set
other variables that are needed by the method.

> 3.How does one properly cross the chasm from rendering variables to
> evaluating them in expressions.

Hopefully the previous info will help. Basically, once you know python
expression syntax, you shouldn't really have a problem. If you know python
expression syntax *and* understand how the namespace is set up, you'll
really be all set...

Kevin