[ZPT] Two column ZPT output from SQL

Tino Wildenhain tino at wildenhain.de
Tue Oct 5 05:53:59 EDT 2004


Hi,

On Tue, 2004-10-05 at 11:24, Duane Raymond wrote:
> Hi,
> 
> I'm trying to get the results of a ZSQL query and present it in
> multiple columns via ZPT.
> 
> I found the following code which does it with a range and have been
> trying to adapt it with no luck so far:
> Original code:
> <table tal:define="items python:range(9);width python:2" border=1>
> <tr tal:repeat="j python:range(0, len(items), width)">
>  <td tal:repeat="i python:items[j:j+width]" tal:content="i"></td>
> </tr>
> </table>
> 
> My Code:
> <table tal:define="items python:container.sql_getMyList();width
> python:2" border=1>
> <tr tal:repeat="j python:range(0, len(items), width)">
>  <td tal:repeat="i python:items[j]" tal:content="i"></td>
> </tr>
> </table>
> 
> Unfortunately it only returns one column with half the data in.  I
> know there are issues with not calling the column name, with returning
> objects vs ranges, etc. - but I can't figure it out to make it work.
> 
> Can anyone give some input?

Dont try to do this all in ZPT. It leads to very ugly code (as 
shown above)

Use a pyton script to prepare the values:

columns=4 # or how many columns you need

qresults=context.sql_getMyList().dictionaries() 
# this is done to get a real list

return [qresults[index:index+columns] for index in
range(0,len(qresults),columns)]

You can use the script like this in ZPT:

<table>
 <tr tal:repeat="row here/theScript">
   <td tal:repeat="col row"
tal:content="col/columnanefromdatabase">samplvalue</td>
 </tr>
</table>

Thats all.

Regards
Tino




More information about the ZPT mailing list