[Zope] ZPT: building to column table

Clemens Klein-Robbenhaar zope@zope.org
Thu, 12 Sep 2002 01:08:33 +0200 (CEST)


Hi Jochen,

 > Hi list,
 > 
 > is there a way to build a table holding two columns of results???
 > 
 > My first try was like this:
 > 
 > 1 	<table border="1">
 > 2 	 <metal:block tal:repeat="result batch" >
 > 3 		 <metal:block tal:define="resultObject result/getObject">
 > 4 			 <metal:block tal:condition="repeat/result/odd">
 > 5 			 <tr>
 > 6 				 <td>
 > 7 					left object
 > 8 				 </td>
 > 9 			 </metal:block>
 > 10
 > 11 			 <metal:block tal:condition="repeat/result/even">
 > 12 				<td>
 > 13 					right object
 > 14 				</td>
 > 15 			 </tr>
 > 16 			 </metal:block>
 > 17 		 </metal:block>
 > 18 	 </metal:block>
 > 19 	</table>
 > 
 > 
 > Bute TALES does not like the </tr> at line 15 because of illegal nesting.
 > 

I agree - it is illegal nesting ;->

I guess there is a good reason why it cannot be implemented easily:
What do You do with the last cell, if there is an uneven number of
elements in the batch ? Currently You try an approach wich will not
close the last <tr>-tag.

You could force it by hiding the bad nesting e.g. inside a script,
something like:

<metal:block tal:repeat="result batch">
   <metal:block tal:define="resultObject result/getObject">
     <metal:block tal:condition="repeat/result/odd"
        tal:replace="structure python:here.render_left(repeat['result'])"  />
     <metal:block tal:condition="repeat/result/even" 
        tal:replace="structure python:here.render_right(repeat['result'])" >
    </metal:block>
</metal:block>

where "render_left" contains something like:

  return  "<tr><td>left object</td>"

and 'render_right" similar.


However I argue instead to reorder the results into two-tuples.
e.g. define a script "pairwise" accepting one parameter "a":

if len(a)%2==1:
  # uneven list length: add a dummy
  a.append("empty value")
return [ (a[i],a[i+1]) for i in range(0, len(a)-1,2) ]


and then do:

 <metal:block tal:repeat="result python:here.pairwise(batch)">
   <tr>  
      <td tal:content="python:result[0]" />
      <td tal:content="python:result[1]" />
   </tr>
 </metal:block>

I guess TAL will like that (modulo syntax errors)

Cheers,
Clemens