[ZPT] Fiddly repeat

Tino Wildenhain tino at wildenhain.de
Wed Sep 24 01:33:40 EDT 2003


Hi Charlie,

Charlie Clark wrote:
> Dear list,
> 
> I'd like to be able to loop through a result-set in a table and have say 
> three columns per row for the same results. What is the best way to do 
> this? I've tried the following which for pretty obvious reasons doesn't 
> work.
> 
> <table tal:define="items here/query">
> 	<div tal:repeat="item items">
> 		<tr tal:condition="python: test(repeat['item'].number() % 3 == 0, 1 ,0)">
> 			<td tal:content="item/value"></td>
> 		</tr>
> 	</div>
> </table>
> 
> Are there any alternatives using tal or is it best to do this in a 
> PythonScript?

Since the philosophy behind TAL is to have a template what matches
the desired output as much as possible, you could just tripple
the td tag.
If you have to calculate the amount of columns somewhere,
you can let td repeat to this number.

<table>
   <tr tal:repeat="row here/query">
    <td tal:repeat="dummy python:range(3)" tal:content="item/value">123</td>
   </tr>
</table>


Your code however seems to be doing completely different,
beside not working and if it would, producing illegal HTML :-)

If you want to distribute a linear array into n*m dimensions,
one way would be:

results=context.query()

columns=3

return [[x.value for x in results[lstart:lstart+columns]] for lstart in 
range(0,len(results),columns)]


and in the corresponding ZPT:

<table>
   <tr tal:repeat="row here/yourScript">
     <td tal:repeat="col row" tal:content="col">123 </td>
   </tr>
</table>



HTH
Tino Wildenhain

     <td




More information about the ZPT mailing list