[Zope] checking for existing/nonexisting properties and objects?

chas panda@skinnyhippo.com
Mon, 13 Sep 1999 01:11:27 +0800


At 06:25 PM 9/12/99 +1000, Darran Edmundson wrote:
>
>Consider the following dtml code fragment:
>
>  <dtml-in "objectValues(['Image'])">
>     <img src="<dtml-var id>">
>     <dtml-if "caption">
>        <caption><dtml-var caption></caption>
>     </dtml-if>
>  </dtml-in>
>
>I use it to loop over all images in a folder,
>displaying the image and its caption if such
>a property exists.  This snippet fails if
>the image doesn't have a "caption" property.

That's actually correct Python functionality.
<dtml-if caption> is like saying :
"If the variable 'caption' has a value equal to None or '' or 0"
which assumes that the variable 'caption' exists.

Try <dtml-if caption> (note, no quotes!). That should work.
The difference is (and somebody pls correct me if I'm wrong) that
this is not using a Python expression and DTML's <dtml-if var_name> 
does both a check for existence as well as check of the value.

Bottomline : use <dtml-if caption> 

>I've been working around this by adding an
>empty caption further up the tree but I'd like
>to avoid this if possible.

Sidenote, if you're cataloging a lot of images, you might find it 
easier to create a zclass called, say, "catalogedimage" with 
2 properties : 'mycaption' and 'myimage'. Then you wouldn't have
to worry about existence of a property, just the value. But maybe 
I'm just overboard with zclasses lately so ymmv.

chas