[ZPT] Fiddly repeat

Tino Wildenhain tino at wildenhain.de
Wed Sep 24 15:42:35 EDT 2003


Hi Charlie,

Charlie Clark wrote:
> 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.

I know. But this was _exactly_ what you wrote.
However I guessed this was not what you intended :)

> 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! ;-) 

Where did you learn HTML? May be you should finally throw your copy
of M$ Frontpage away? ;))

>  
> 
>>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?

Hm. ok, this example works. But you can write it like this:


ol=[]
for lstart in range(o,len(results),columns):
     ol.append(results[lstart:lstart+columns])

return ol

and use col/value in the tal:content attribute

But list comprehensions only look complicated at start,
you easyly get used to them once you try them a bit.

(start interactive python and play around with it)
> 
> 
>>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.

Sure it is, its a perfect 1 row 1 column table.
You can write "dummy" tds/trs around it and
disable them with tal:replace="nothing"
or tal:condition="nothing">

> 
> Thanx for the tip

OTOH, I even guess you want to do all this
because you want to display Images in rows
and columns. Am I right? :)

This is even better done with just a tal:repeat
over your original results and CSS.

See http://www.alistapart.com/stories/practicalcss/
for how to do it.


Regards
Tino Wildenhain




More information about the ZPT mailing list