[ZPT] tal:repeat and variables
Clemens Robbenhaar
zpt@zope.org
Wed, 12 Feb 2003 13:11:05 +0100
[...]
> > without resorting to (ugly) global variables, is there a way to
> > pass values
> > from one iteration of tal:repeat to another?
> >
> > I have a database query giving me a table like:
> >
> > Topic | Name
> > --------------------
> > Computers | John
> > Computers | Smith
> > Computers | Dilvert
> > Software | Peter
> > Software | Mary
> >
> > and I would like to show it like
> >
> > Topic | Name
> > --------------------
> > Computers | John
> > | Smith
> > | Dilvert
> > Software | Peter
> > | Mary
>
> It should be possible to do this using something like (assuming row variable
> is 'item'):
>
> <td tal:content="python:('',item.topic)[repeat['item'].start]">topic</td>
>
Hm, this works only if the result set is split into two sequences, one
containing all "Computers" and one containing all "Software" people, or
did I miss something here ?
> There's probably a better way though. That kinda makes my head hurt.
I don't think there is a better way. The tal:repeat is just designed in
a way that all elements in a repeat are rendered nearly independent of
each other.
Despite it is actually presentation logic and not application logic, I
would tend to filter the result set through a python script, replacing
the unwanted entries by empty strings. I did not work with SQL result
sequences, but I guess one maybe has to make a copy of the result
sequence, as the original sequence may be immutable.
Thus two drawbacks for this solution. Maybe global variables are simply
the best way. (As they are only "global" to the current page this may
not be that bad as it seems at first glance.)
Cheers,
Clemens