[Zope] Help with dtml-let & dtml-if

Michel Pelletier michel@digicool.com
Wed, 29 Mar 2000 17:17:23 -0800


Andrew Diller wrote:
> 
> Hello:
> 
> I'm not getting dtml-let. I use it to create a variable that I can access, correct?

Yes.
 
> Say I want to initialize a counter, and change it from 0 1 each iteration of a loop.

Let me warn you, you are allready thinking too procedurally.  Remeber
DTML is a _template_ language.
 
> This will print out rows of a table with alternating background colors:
> 
> <dtml-let x=0>
> </dtml-let>
> 
> <dtml-in "getElementsByTagName('book')" >
> 
> <dtml-if x==0>
>         <tr bgcolor="DDEEAC">
> <dtml-else>
>         <tr>
> </dtml-if>
> 
> write out some html
> 
> </tr>
> 
> <dtml-if x==0>
>         <dtml-let x=1>
>         </dtml-let>
> <dtml-else>
>         <dtml-let x=0>
>         </dtml-let>
> </dtml-if>
> 
> </dtml-in>
> </table>
> </dtml-with>

Let defines a new scope for variables, the scope being defined by the
let block.  Each time you use the let tag, you are creating and then
immediatly destroying the scope of your variable.  If you want to use
'x' in your DTML code, that code must be contained in the let block that
defines x.

What you are doing is easily done using the 'sequence-even' attribute.

<dtml-in "getElementsByTagName('book')" >
  <dtml-if sequence-even>
    Print stuff for even rows
  <dtml-else>
    Print stuff for odd rows.
  </dtml-if>
</dtml-in>

-Michel