[Zope] Alternate row shading

Steve Jibson stevej at parlant.com
Wed Jun 9 19:31:40 EDT 2004


The documentation that I've been able to find suggests that if I want to 
do alternate-row shading for a table, I should do the following:

<table>
   <tbody tal:repeat="item container/objectValues">
     <tr bgcolor="#EEEEEE" tal:condition="repeat/item/even">
       <td tal:content="repeat/item/number">#</td>
       <td tal:content="item/id">Id</td>
       <td tal:content="item/meta_type">Meta-Type</td>
       <td tal:content="item/title">Title</td>
     </tr>
     <tr tal:condition="repeat/item/odd">
       <td tal:content="repeat/item/number">#</td>
       <td tal:content="item/id">Id</td>
       <td tal:content="item/meta_type">Meta-Type</td>
       <td tal:content="item/title">Title</td>
     </tr>
   </tbody>
</table>

This is all fine and dandy until the number of columns gets big and the 
code to generate them gets complicated.  Since all of the TDs are 
duplicated, you end up maintaining the same code twice.

It was simpler in DTML when I could do:

...
<tr<dtml-if sequence_even> bgcolor="#EEEEEE"</dtml-if>>
...

and not duplicate the whole block of TDs.


I think I've found a way to make it work similarly using ZPT, but it's a 
hack at best, and it's very ugly:

<table>
   <tr tal:repeat="item container/objectValues"
       tal:attributes="bgcolor python:(repeat['item'].even()
                                       and '#EEEEEE') or None">
     <td tal:content="repeat/item/number">#</td>
     <td tal:content="item/id">Id</td>
     <td tal:content="item/meta_type">Meta-Type</td>
     <td tal:content="item/title">Title</td>
   </tr>
</table>

I'd say this is still better than duplicated code.  I've been doing this 
ZPT stuff for all of two days now.  Is there a better way to do this?




More information about the Zope mailing list