[Zope] TAL Syntax for Iterating Over List of Dictionaries

Chris McDonough chrism@zope.com
Thu, 06 Jun 2002 08:09:29 -0400


Untested:

<table>
<th>Title</th>
<th>Summary</th>
<tbody tal:repeat="item articleList">
<tr><td tal:content="item/title"></tr>
<tr><td tal:content="item/Summary"></tr>
</tbody>
</table>


Dan Shafer wrote:
> Given a Python script that returns a list of dictionaries that looks 
> like this:
> 
> [{'title': 'Article 1', 'Summary': 'Summary of Article 1'} {'title': 
> 'Article 2', 'Summary': 'Summary of Article2'}]
> 
> and the need to create a table in a ZPT template that has the key of 
> each dictionary in column 1 and the summary of each dictionary in column 
> 2, could someone give me correct tal syntax? I think I can see how this 
> would work if I were returning a single dictionary (though I haven't 
> been able to get that to quite work yet) but I can't find anything about 
> iterating over a list of dictionaries from TAL.
> 
> For completeness, here's the Python script. It iterates over all objects 
> in a folder from which it is run. Obviously it needs error-checking and 
> other doodads, but hopefully this is enough to enable someone to help me 
> here.
> 
> Thanks.
> 
> articleList=[]
> for x in context.objectValues():
>   articleInfo={}
>   articleInfo['title']= x.getProperty('title')
>   articleInfo['Summary'] = x.getProperty('Summary')
>   articleList.append(articleInfo)
> return articleList
> 
> (And, yes, I do have this working as a pure ZPT page. I'm trying to 
> learn to use Python from ZPT correctly for cases where larger amounts of 
> information or more complex pre-processing rule out pure ZPT solutions.)
> 
> 
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce
> http://lists.zope.org/mailman/listinfo/zope-dev )


-- 
Chris McDonough                    Zope Corporation
http://www.zope.org             http://www.zope.com
"Killing hundreds of birds with thousands of stones"