[Zope] <dtml-if> String question

Martijn Pieters mj@digicool.com
Mon, 15 May 2000 21:25:14 +0200


On Mon, May 15, 2000 at 11:46:25AM -0700, Stuart Foster wrote:
> Here's a nubie question. If I am using two string values in a <dtml-if> tag
> why is it not evaluating to true?
> 
> s1 = 'Hello'
> s2 = 'Hello'
> 
> <dtml-if "s1==s2">  <-- This is returning 0

Are you sure you are comparing two strings? The only explanation I see is that
the above is just a simplified testcase of what you think is happening.

When you compare, for example 'id' with 'foo', where <dtml-var foo> renders
'foo', "id=='foo'" will still yield false. This is because 'id' is, on many
objects, a method, while on others it is a property. In this example you'd be
comparing the method object with a string, which always yields false, while
the comparison "id()=='foo'" would yield true!

Now, in order to allow for both cases, one where id is a string property (and
can be compared using "id=='foo'", and the case where id is a method, use the
following syntax:

  "_['id'] == 'foo'"

This is, under the hood, the same as <dtml-var id>, which will call id if it
is a method, and otherwise will just return the value of the 'id' property or
variable.

-- 
Martijn Pieters
| Software Engineer    mailto:mj@digicool.com
| Digital Creations  http://www.digicool.com/
| Creators of Zope       http://www.zope.org/
|   The Open Source Web Application Server
---------------------------------------------