Lists and external methods
Hello there, I started working with Zope a few days ago, and I'm currently having some troubles with external methods. I made an external method that returns a record like list, but this doesn't seem to work correctly> The list look a bit like this: 90, "x86", "Explanation", "22/06/200" 89, "x86", "Explanation", "22/06/200" 89, "mac", "Explanation", "22/06/200" I want the external method to return this in a list, where I can iterate over using the <dtml-in> tag, so that I can reference the different fields in each record with a name. What should the list look like so that I can accomplish this? Pieter
On Tue, 4 Jul 2000, Pieter Claerhout wrote:
I want the external method to return this in a list, where I can iterate over using the <dtml-in> tag, so that I can reference the different fields in each record with a name.
What should the list look like so that I can accomplish this?
dtml-in eats a list of objects or a list of dictionaries. Example 1. class Item: pass Your external methods should return: l = [] item1 = Item() item1.date = "21/12/67" item1.foo = "bar" l.append(item1) item2 = Item() item2.date = "1/1/2000" item2.foo = "baz" l.append(item2) ...etc... return l In the DTML use the list: <dtml-in thelist><dtml-var date></dtml-in thelist> Example 2 (dictionaries). Your external methods should return: l = [] item1 = {} item1["date"] = "21/12/67" item1["foo"] = "bar" l.append(item1) item2 = {} item2["date"] = "1/1/2000" item2["foo"] = "baz" l.append(item2) ...etc... return l In the DTML use the list: <dtml-in thelist mapping><dtml-var date></dtml-in thelist> ^^^^^^^ dtml-in needs to be notified these are dicttionaries. Oleg. (All opinions are mine and not of my employer) ---- Oleg Broytmann Foundation for Effective Policies phd@phd.russ.ru Programmers don't die, they just GOSUB without RETURN.
Oleg, Thanks for that. It's the clearest explanation I've seen as to what 'mapping' does (and I've read the docs a fair bit) cheers tone. At 1:00 pm +0000 4/7/00, Oleg Broytmann wrote:
On Tue, 4 Jul 2000, Pieter Claerhout wrote:
I want the external method to return this in a list, where I can iterate over using the <dtml-in> tag, so that I can reference the different fields in each record with a name.
What should the list look like so that I can accomplish this?
dtml-in eats a list of objects or a list of dictionaries.
Example 1.
class Item: pass
Your external methods should return:
l = []
item1 = Item() item1.date = "21/12/67" item1.foo = "bar" l.append(item1)
item2 = Item() item2.date = "1/1/2000" item2.foo = "baz" l.append(item2)
...etc...
return l
In the DTML use the list: <dtml-in thelist><dtml-var date></dtml-in thelist>
Example 2 (dictionaries).
Your external methods should return:
l = []
item1 = {} item1["date"] = "21/12/67" item1["foo"] = "bar" l.append(item1)
item2 = {} item2["date"] = "1/1/2000" item2["foo"] = "baz" l.append(item2)
...etc...
return l
In the DTML use the list: <dtml-in thelist mapping><dtml-var date></dtml-in thelist> ^^^^^^^ dtml-in needs to be notified these are dicttionaries.
Oleg. (All opinions are mine and not of my employer)
Dr Tony McDonald, FMCC, Networked Learning Environments Project http://nle.ncl.ac.uk/ The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
Hi, Oleg Broytmann wrote:
On Tue, 4 Jul 2000, Pieter Claerhout wrote:
I want the external method to return this in a list, where I can iterate over using the <dtml-in> tag, so that I can reference the different fields in each record with a name.
What should the list look like so that I can accomplish this?
dtml-in eats a list of objects or a list of dictionaries.
I usually use the following helper-class: class generic_datarecord: " This class provides an overall mapping of value pairs " def __init__(self,**data): self.data=data def __len__(self): return(len(self.data.keys())) def __getitem__(self,key): return(self.data[key]) def __getattr__(self,key): return self.__getitem__(key) def keys(self): return(self.data.keys()) def values(self): return(self.data.values()) if you use generic_datarecord(column1=value1,coulumn2=value2) for each row, you can alterate over a list of such datarecord_objects with <dtml-in> using <dtml-var column1> and <dtml-var column2> HTH Tino Wildenhain PS: __len__() is optional, but for easy use in all circumstances (e.g. <dtml-in> over a single datarecord)
participants (4)
-
Oleg Broytmann -
Pieter Claerhout -
Tino Wildenhain -
Tony McDonald