[Zope] sort list with ZPT

hans hans@beehive.de
Mon, 03 Jun 2002 17:13:10 +0100


Nico de Boer wrote:

> Hi all,
>
> I want to sort my objectValues('News') at date_add, the date that the
> News objects were added.
>
> I have written a python method to do this:
>
>     def list_peers(self, sortkey=3D'id'):
>         """returns a listing of peer news objects sorted on sortkey"""
>         itemlist =3D []
>         for item in self.REQUEST['PARENTS'][1].objectValues('GNAW'):
>             itemlist.append(
>                 (getattr(item,sortkey),item)
>                 )
>         itemlist.sort()
>         return itemlist
>
> I can give the sortkey as an argument to the method.
>
> When I put the following lines:
>
> <div tal:define=3D"itemlist python:here.list_peers(sortkey=3D'date_add'=
)">
> <span tal:repeat=3D"item itemlist"><p tal:content=3D"item"></p></span>
>
> in my ZPT I get the following output:
>
> (<News instance at 92c0e48>, '29 may 2002')
>
> (<News instance at 92c0f10>, '30 may 2002')
>
> (<News instance at 92c0418>, '31 may 2002')
>

the above code gives you the items themselves.
but zope tries to render them into strings (html is string, only=B4!),
and calls the __str__-method of item.
since there is none, python renders an Ersatz-representation,
which is <News instance at yyyyy>.
The object you have, but you might want its name (=3Did)

> Now I want to implement this in ZPT as follows:
>
> <div tal:define=3D"itemlist python:here.list_peers">
> <div tal:repeat=3D"item itemlist">
>     <a href=3D"#"
>     tal:attributes=3D"href item/absolute_url"
>     tal:content=3D"item/title">Title news</a> </div>
> </div>
>
> But I get Undefined Errors. How can I get to the items in the itemlist?

what undefined errors? do those items have a title?
did you try title_or_id?
--
-------------------------------------------------------------
Who's got only a hammer sees the world as a nail
hans augustin  (software developer)           hans@beehive.de
beehive elektronische medien GmbH       http://www.beehive.de
phone: +49 30 847-82 0                  fax: +49 30 847-82 299