[Zope] dtml-in over the output from my method

Max M maxmcorp@worldonline.dk
Mon, 6 Nov 2000 22:48:40 +0100


From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Tim Hicks

>I am trying to create a zope product in python but have got stuck with
using the
>dtml-in tag to iterate over a method (or is it a function? I get a little
mixed
>up) of my class. Here is the method,

def list_messages(self):
    msgList = []
                lr = open(self.user_dir+'/msg_list', 'r')
                spl = re.compile('\s\|\s')
                for msg_line in lr.readline():
                        num_from_sub = []
                        num_from_sub = spl.split(msg_line, 2)
                        print num_from_sub
                        self.msgnum = num_from_sub[0]
                        self.msgfrom = 'This is from UNKNOWN'
                        self.msgsub = 'My special subject'
                        return self.msgnum, self.msgfrom, self.msgsub
     return msgList

The dtml-in does not call your method for every iteration, it just calls
once and then expects a list in return. What you need to do is to create a
method that returns a list of objects:

def list_messages(self):

    class msg:
        def __init__(self, msgnum, msgfrom, msgsub):
            self.msgnum  = msgnum
            self.msgfrom = msgfrom
            self.msgsub  = msgsub

    msgList = []
    lr = open(self.user_dir+'/msg_list', 'r')
    spl = re.compile('\s\|\s')
    for msg_line in lr.readline():
        num_from_sub = []
        num_from_sub = spl.split(msg_line, 2)

        # Oh no ... this wont cut it!
        #self.msgnum = num_from_sub[0]
        #self.msgfrom = 'This is from UNKNOWN'
        #self.msgsub = 'My special subject'
        #return self.msgnum, self.msgfrom, self.msgsub

        # Do it like this:
        msgList.append(msg(num_from_sub[0],'This is from UNKNOWN',
                           'My special subject'))
     return msgList