[Zope] ZPT: building to column table

Sascha Welter welter@network-ag.com
Thu, 12 Sep 2002 12:41:29 +0200


Jochen Haeberle schrieb:

> is there a way to build a table holding two columns of results???

Hi Jochen,

some time ago I had to look for such a thing, but with 3 columns... what 
I came up with is some mathematics to do a layout that will go for any 
number of columns. I was surprised that I did not already find something 
like this, as I expected this to be a common task.

All html-comments can be left out, I just added them for this mail.


<!-- Just an example list: -->
span tal:define="global imagestring_list python:['1', '2', '3', '4', 
'5', '6', '7']"/>

<!-- Set up the variables, currently 2, can be changed -->
<span tal:define="global num_cols python:2"/>

<!-- WARNING: Math here. We calculate the number of rows that we will 
need :-) -->
<span tal:define="global myrows 
python:range(0,(len(imagestring_list)+num_cols-1)/num_cols)"/>
<!-- we calculate the number of empty cells we need to fill up -->
<span tal:define="global addemptycells python:(num_cols - 
(len(imagestring_list)%num_cols))%num_cols"/>

<table border="1">
<tr tal:repeat="reihe myrows">
<!-- we grab just those items we need for this row -->
<td tal:repeat="item python:imagestring_list[reihe*num_cols : 
reihe*num_cols+num_cols]">
Content: <span tal:replace="item"/>

<!-- we cheat in some empty cells according to our needs,
      only at the end of the table -->
<span tal:condition="python:repeat['item'].end and repeat['reihe'].end 
and (addemptycells>=1)"
             tal:replace="structure 
python:'&lt;/td&gt;&lt;td&gt;&amp;nbsp;'*addemptycells" />

</td>
</tr>
</table>

Have fun! (And watch out for line breaks inserted by the mail program.)

Regards,

Sascha


-- 
Sascha Welter           <mailto:welter@network-ag.com>
Network AG              Programmer, Sysop, IT-Support, BOFH
Ruetistrasse 17         Tel. 01 755 40 20    Mob. 079 263 34 16
CH-8952 Schlieren       <http://www.network-ag.com>