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. Another try would be to save the odd object in a variable and build the total row in the even run. I could not get this to work, either... i would get errors or duplicate entries on both sides. I guess python works with pointers the same like Java does??? I guess I am stuck (again)... anyone has an idea for me please??? Jochen
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
Hi Clemens,
Hi Jochen,
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
Thanks a lot, works nearly like a charm :-) regards Jochen
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>
This works basically fine, except that my "batch" holds complex objects I get from the portal_catalog. So I added tal:define="oddObj python:result[0];evenObj python:result[1] Getting attributes from those objects works fine for most part, except when I try to build a url like <a href="/view" tal:attributes="href oddObj/absolute_url"> or a href="view" tal:attributes="href string:${evenObj/id}/sfbdok_view"> the generated HTML has "portal_catalog" in it... why's that and what can I do about it? Thanks Jochen
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:'</td><td> '*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>
Jochen Haeberle wrote:
is there a way to build a table holding two columns of results???
If, as the rest of this thread suggests, you want your table arranged like this: element0 element1 element2 element3 element4 ...then I recommend http://www.zopelabs.com/cookbook/998066576 A similar technique could be used to produce: element0 element3 element1 element4 element2 Cheers, Evan @ 4-am
participants (4)
-
Clemens Klein-Robbenhaar -
Evan Simpson -
Jochen Haeberle -
Sascha Welter