[Zope] TAL Syntax for Iterating Over List of Dictionaries
Dan Shafer
pydan@danshafer.com
Thu, 06 Jun 2002 00:56:30 -0700
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.)