[ZPT] Fiddly repeat

Charlie Clark charlie at begeistert.org
Wed Sep 24 07:57:04 EDT 2003


Hey Tino,

On 2003-09-24 at 07:33:40 [+0200], Tino Wildenhain wrote:
> > <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>
 
Imagine my results are ["one", "two", "three"...]
This will give me three times a single value per row.

ie.
<tr>
<td>one</td><td>one</td><td>one</td>
</tr>
<tr>
<td>two</td><td>two</td><td>two</td>
</tr>
<tr>
<td>three</td><td>three</td><td>three</td>
</tr>

but I want

<tr>
<td>one</td><td>two</td><td>three</td>
</tr>
<tr>
<td>four</td><td>five</td><td>six</td>
</tr>
<tr>
<td>seven</td><td>eight</td><td>nine</td>
</tr>

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

It's not quite cut and paste what I tried last night but prety close and it 
worked (functionally). "Usable" HTML is virtually always illegal, always 
has been, always will be! ;-) 
 
> 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)]

So I do need a Script? Thought so but pity in view of the fairly recent 
discussion re. alternating rows. I'll try and get my head around the list 
"in"comprehension and get it to work. In my experience this isn't too wild 
a requirement. Possibly the sort of thing for ZTUtils?

> and in the corresponding ZPT:
> 
> <table>
>    <tr tal:repeat="row here/yourScript">
>      <td tal:repeat="col row" tal:content="col">123 </td>
>    </tr>
> </table>

Interestingly, this solution doesn't have template source which is similar 
to result source.

Thanx for the tip

Charlie



More information about the ZPT mailing list