TAL Syntax for Iterating Over List of Dictionaries
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.)
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"
Thanks, Chris, but where in the TAL do I invoke the ListAllArticlesDict script that generates the list of dictionaries? IOW, how do I *get* the articleList structure into the ZPT so I can iterate over it? That's where I'm stuck. At 08:09 AM 6/6/2002 -0400, Chris McDonough wrote:
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"
<table> <th>Title</th> <th>Summary</th> <tbody tal:repeat="item ListAllArticlesDict"> <tr><td tal:content="item/title"></tr> <tr><td tal:content="item/Summary"></tr> </tbody> </table> HTH, - C On Thu, 2002-06-06 at 13:26, Dan Shafer wrote:
Thanks, Chris, but where in the TAL do I invoke the ListAllArticlesDict script that generates the list of dictionaries? IOW, how do I *get* the articleList structure into the ZPT so I can iterate over it? That's where I'm stuck.
At 08:09 AM 6/6/2002 -0400, Chris McDonough wrote:
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"
_______________________________________________ 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 )
Your repeat line should look something like this: <tbody tal:repeat="item python:scriptThatReturnsArticleList()"> HTH, Chris Meyers On Thu, Jun 06, 2002 at 10:26:19AM -0700, Dan Shafer wrote:
Thanks, Chris, but where in the TAL do I invoke the ListAllArticlesDict script that generates the list of dictionaries? IOW, how do I *get* the articleList structure into the ZPT so I can iterate over it? That's where I'm stuck.
At 08:09 AM 6/6/2002 -0400, Chris McDonough wrote:
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"
_______________________________________________ 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 )
participants (3)
-
Chris McDonough -
Chris Meyers -
Dan Shafer