List within a list - how to use in a page template
I am trying to get my head around representing a list in a page template. Assume the following list:
m [[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0], [61282125378L, 1, 6, 0], [61282125374L, 1, 6, 0] m[0] [61282125371L, 1, 6, 0] m[0][0] 61282125371L
i.e. m is a list where each member is itself a list. How do I get a page template to render a particular item? Assume a function getem returns the list. <tal:block repeat="m python:here.getem()"> <td tal:repeat="single m" tal:content="single"></td> This code will produce four columns and five rows, with each populated by the list members. But what if I only want the first and third members in each case i.e. 2 columns and five rows? I thought it would be something like tal:content="python:single[0]" but that gives me an unsubscriptable object error. Any suggestions would be appreciated. Cameron
On Wed, Dec 14, 2005 at 06:06:21PM +1300, Cameron Beattie wrote:
I am trying to get my head around representing a list in a page template.
Assume the following list:
m [[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0], [61282125378L, 1, 6, 0], [61282125374L, 1, 6, 0] m[0] [61282125371L, 1, 6, 0] m[0][0] 61282125371L
i.e. m is a list where each member is itself a list.
How do I get a page template to render a particular item? Assume a function getem returns the list. <tal:block repeat="m python:here.getem()"> <td tal:repeat="single m" tal:content="single"></td>
This code will produce four columns and five rows, with each populated by the list members. But what if I only want the first and third members in each case i.e. 2 columns and five rows?
I thought it would be something like tal:content="python:single[0]" but that gives me an unsubscriptable object error.
That's because, in your example, each time through the loop, single is an int. Try indexing m instead. -- Paul Winkler http://www.slinkp.com
Paul Winkler wrote:
On Wed, Dec 14, 2005 at 06:06:21PM +1300, Cameron Beattie wrote:
I am trying to get my head around representing a list in a page template.
Assume the following list:
m
[[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0], [61282125378L, 1, 6, 0], [61282125374L, 1, 6, 0]
m[0]
[61282125371L, 1, 6, 0]
m[0][0]
61282125371L
i.e. m is a list where each member is itself a list.
How do I get a page template to render a particular item? Assume a function getem returns the list. <tal:block repeat="m python:here.getem()"> <td tal:repeat="single m" tal:content="single"></td>
This code will produce four columns and five rows, with each populated by the list members. But what if I only want the first and third members in each case i.e. 2 columns and five rows?
I thought it would be something like tal:content="python:single[0]" but that gives me an unsubscriptable object error.
That's because, in your example, each time through the loop, single is an int. Try indexing m instead.
I think all thats needed is a tal:condition (he wants to filter out rows) 1)<span tal:define="res python: context.pyTest();" <--- returns list of lists like [[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0]] tal:repeat="r1 res"> 3) <tal:span tal:repeat = "r2 r1" 4) tal:condition="python: repeat.r2.number in (1,2,4)" 5) tal:content="r2"> 6) </tal:span> Where 4 is not brain dead as in my example. David
David H wrote:
Paul Winkler wrote:
On Wed, Dec 14, 2005 at 06:06:21PM +1300, Cameron Beattie wrote:
I am trying to get my head around representing a list in a page template.
Assume the following list:
m
[[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0], [61282125378L, 1, 6, 0], [61282125374L, 1, 6, 0]
m[0]
[61282125371L, 1, 6, 0]
m[0][0]
61282125371L
i.e. m is a list where each member is itself a list.
How do I get a page template to render a particular item? Assume a function getem returns the list. <tal:block repeat="m python:here.getem()"> <td tal:repeat="single m" tal:content="single"></td>
This code will produce four columns and five rows, with each populated by the list members. But what if I only want the first and third members in each case i.e. 2 columns and five rows?
I thought it would be something like tal:content="python:single[0]" but that gives me an unsubscriptable object error.
That's because, in your example, each time through the loop, single is an int. Try indexing m instead.
I think all thats needed is a tal:condition (he wants to filter out rows)
1)<span tal:define="res python: context.pyTest();" <--- returns list of lists like [[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0]] tal:repeat="r1 res"> 3) <tal:span tal:repeat = "r2 r1" 4) tal:condition="python: repeat.r2.number in (1,2,4)" 5) tal:content="r2"> 6) </tal:span>
Where 4 is not brain dead as in my example.
David
Ok, its best to parse your list of lists in a python script. You pass a tuple (or array) of inclusionary index numbers. It will return the filtered list of lists The problem with the try I gave before is that *condition* is evaluated before *repeat* :-0 So ... # python script # input: a tuple named tup of index items to include in list of lists, eg (1,3) and the list of lists #example input: listoflists = [[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0]] #example tup: (1,3) ret = [] for i in range(len(listoflists)): if i in tup: ret.append(x[i]) return ret Then your ZPT is easy. David
David H wrote:
# python script # input: a tuple named tup of index items to include in list of lists, eg (1,3) and the list of lists #example input: listoflists = [[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0]] #example tup: (1,3)
ret = [] for i in range(len(listoflists)): if i in tup: ret.append(x[i]) return ret
Hmmm, I'd hypothesise that whatever is returning that list of lists is what needs to change :-S Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
David H wrote:
# python script # input: a tuple named tup of index items to include in list of lists, eg (1,3) and the list of lists #example input: listoflists = [[61282125371L, 1, 6, 0], [61282125379L, 1, 6, 0], [61282825240L, 6, 6, 0]] #example tup: (1,3)
ret = [] for i in range(len(listoflists)): if i in tup: ret.append(x[i]) return ret
Hmmm, I'd hypothesise that whatever is returning that list of lists is what needs to change :-S
Thanks. I'm using a web service which returns the list. I resolved the problem by iterating through the returned list and populating a new list. I'm sure there's a better way, I just couldn't figure it out. Cameron
Cameron Beattie wrote:
Thanks. I'm using a web service which returns the list.
What do the calls to this web service look like?
I resolved the problem by iterating through the returned list and populating a new list. I'm sure there's a better way, I just couldn't figure it out.
Indeed :-S Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
participants (4)
-
Cameron Beattie -
Chris Withers -
David H -
Paul Winkler