[Zope] <dtml-if> question

Curtis Maloney curtis@cardgate.net
Fri, 23 Feb 2001 12:00:46 +1100


On Friday 23 February 2001 12:58, Hans de Wit wrote:
> Hello,
>
> I am doing something wrong with a dtml-if,
> i have been looking for a few hours and i don't understand
> what is happening.
> What the following code should do is test wheter an image exists and
> if it exists render it in the browser. If it doesn't exist it should show
> an empty default image. I found two ways to do it and both do it wrong in
> different ways
>
>              <dtml-let foto="'/Cis/fotos/afoto.jpg'">
>
>              <dtml-if expr="_.getitem('foto')">
>              <img src="&dtml-foto;">
>              <dtml-else>
>              <img src="/Cis/fotos/Noface.jpg">
>              </dtml-if>
>

OK... In order...

Firstly, "_.getitem('foto')"  says to get the item called "foto", not to get 
the item named in the variable 'foto'.  "_.getitem(foto)" is what you'd want 
for that.

Secondly, getitem() is  returning something other than 0 or None, and so is 
evaluating to true.  If you want to find out if an object exists, you'd do 
better to use "has_key()" like this (untested):

<dtml-if "has_key(foto)">


>              <dtml-if &dtml-foto;>
>              <img src="&dtml-foto;">
>              <dtml-else>
>              <img src="/Cis/fotos/Noface.jpg">
>              </dtml-if>
>
>              </dtml-let>
>

A fairly understandable mistake.  This is much the same as trying to use 
<dtml> tags within <dtml> tags.  &dtml-foo  is shorthand for <dtml-var foo>.  
Again shorthand causes confusion for the new.. *sigh* (o8

Again, an alternative to this might be to try:

<dtml-if "_[foto]">

Which will look in the current namespace for any object matching the name in 
foto.


> The first one allways finds the true in the  <dtml-if> and the second one
> allways the false. Why is that and what should i change?
>
> Met vriendelijke groeten,
>
> Hans de Wit
>
> h.de.wit@scp.nl
>

I hope this has cleared things up a little.

Have a better one,
	Curtis Maloney.