[ZPT] beginner question

Fernando Martins fmartins at hetnet.nl
Tue Apr 22 13:26:01 EDT 2003


Uwe Schmitt wrote:
> I have a flat structure
>      [ {'name':'uwe' }, {'name':'peter'}, {'name': 'jon' }, .... ]
> and I want to render it to a two column table
>    <table>
>    <tr><td>uwe</td><td>peter</td></tr>
>    <tr><td>jon</td><td>.....</td></tr>
>    ...
>    </table>
> Is there a way to do this in ZPT alone, or do I have
> to modify my data ?

This is a quite common need but I'm afraid ZPT has not a nice and clean
solution for it.

What I do is to access the flat structure as a matrix (n x 2, in your case).
For example,

<table tal:define="flat here/getNames;
                   nlines python: len(flat);
                   ncols python: 2">
<tr tal:repeat="i python: range(0, nlines)">
    <td tal:repeat="j python: range(0, ncols)">
        <p tal:condition="python: i*ncols+j < ncl"
           tal:content="python: flat[i*ncols+j]['name']"></p>


PS for the list:

I would prefer to do something like:

<table tal:define="ncols python: 2">
<tr tal:repeat="item here/getNames">
    <td tal:repeat-shift="item ncols">
        <p tal:content="item/name"></p>

This has the problem of dealing with missing elements (as in an odd list),
but looks more readable.

Any thoughts?

More information about the ZPT mailing list