Building a list programmatically under DTML
Hi, I'm working on a project where I need to build a list programmatically I have tried a number of variations of the following without much success. Once the list has been built, I need to pass it on to the next page: <dtml-call "REQUEST.set('idx', 0)"> <dtml-call "REQUEST.set('desc', [])"> <dtml-call "REQUEST.set('desc_qty', [])"> . . . <td> <dtml-var expr="_[Category]"> <dtml-if "_[Category][0:2] <> 'No'"> <dtml-call "REQUEST.set('desc[&dtml-idx;]', _[Category])"> <dtml-call "REQUEST.set('desc_qty[&dtml-idx;]', qty)"> <dtml-call "REQUEST.set('idx', idx + 1)"> </dtml-if> </td> All help would be greatly appreciated! -- Stand Fast, tjg. Red Hat Certified Engineer tjg@avalongroup.net Chief Technology Officer www.avalongroup.net Avalon Technology Group, Inc. (503) 246-3630
>>>>>>Exceptional Minds, Innovative Products<<<<<<<<<<<<
Timothy Grant wrote:
<dtml-var expr="_[Category]"> <dtml-if "_[Category][0:2] <> 'No'"> <dtml-call "REQUEST.set('desc[&dtml-idx;]', _[Category])"> <dtml-call "REQUEST.set('desc_qty[&dtml-idx;]', qty)"> <dtml-call "REQUEST.set('idx', idx + 1)"> </dtml-if>
should be: <dtml-if "_[Category][:2] <> 'No'"> <dtml-call "REQUEST.set('desc[idx]', _[Category])"> <dtml-call "REQUEST.set('desc_qty[idx]', qty)"> <dtml-call "REQUEST.set('idx', idx + 1)"> </dtml-if> Assuming that Category contains an Id, and not the string you are trying to examine and store, that is. If Category is the string, leave off the '_[]'s.
Timothy Grant wrote:
Hi,
I'm working on a project where I need to build a list programmatically
I have tried a number of variations of the following without much success. Once the list has been built, I need to pass it on to the next page:
<dtml-call "REQUEST.set('idx', 0)"> <dtml-call "REQUEST.set('desc', [])"> <dtml-call "REQUEST.set('desc_qty', [])"> . . . <td> <dtml-var expr="_[Category]"> <dtml-if "_[Category][0:2] <> 'No'"> <dtml-call "REQUEST.set('desc[&dtml-idx;]', _[Category])"> <dtml-call "REQUEST.set('desc_qty[&dtml-idx;]', qty)"> <dtml-call "REQUEST.set('idx', idx + 1)"> </dtml-if> </td>
All help would be greatly appreciated!
Hi Timothy, first of all: I can't give you a solution... :-( But maybe I can give you a hint, because I had a similar problem the other day. Unlike PERL, python does not allow you to populate a list by indexing slots that are not populated yet. So the following code:
lst = [] lst[10] = 'foo' will give you the following error: IndexError: list assignment index out of range But as I can see from your code above, that's exactly what you would like to do, and I doubt that DTML-lists behave any different than python lists... Maybe you should switch to an External Method that gives you access to the list object's "append()" method, which allows you to expand an existing (empty) list.
Hope this helps a little, Heiko -- Heiko Stoermer MIG Augsburg
At 10:00 07/09/99 , Heiko Stoermer wrote:
Timothy Grant wrote:
Hi,
I'm working on a project where I need to build a list programmatically
I have tried a number of variations of the following without much success. Once the list has been built, I need to pass it on to the next page:
<dtml-call "REQUEST.set('idx', 0)"> <dtml-call "REQUEST.set('desc', [])"> <dtml-call "REQUEST.set('desc_qty', [])"> . . . <td> <dtml-var expr="_[Category]"> <dtml-if "_[Category][0:2] <> 'No'"> <dtml-call "REQUEST.set('desc[&dtml-idx;]', _[Category])"> <dtml-call "REQUEST.set('desc_qty[&dtml-idx;]', qty)"> <dtml-call "REQUEST.set('idx', idx + 1)"> </dtml-if> </td>
All help would be greatly appreciated!
Hi Timothy, first of all: I can't give you a solution... :-( But maybe I can give you a hint, because I had a similar problem the other day. Unlike PERL, python does not allow you to populate a list by indexing slots that are not populated yet. So the following code:
lst = [] lst[10] = 'foo' will give you the following error: IndexError: list assignment index out of range But as I can see from your code above, that's exactly what you would like to do, and I doubt that DTML-lists behave any different than python lists... Maybe you should switch to an External Method that gives you access to the list object's "append()" method, which allows you to expand an existing (empty) list.
I don't think you need an EM for that. Just try <dtml-call "desc.append(_[Category])"> <dtml-call "desc_qty.append(qty)"> You don't need the idx var at all this way. You could also try a dictionary: <dtml-call "REQUEST.set('desc', {})"> . . . <td> <dtml-var expr="_[Category]"> <dtml-if "_[Category][0:2] <> 'No'"> <dtml-call "desc[_[Category]] = qty"> </dtml-if> </td> And you can later loop over the keys() or items() methods of the dictionary. One caveat: this is untested. YMMV. -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-7502100 Fax: +31-35-7502111 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
I don't think you need an EM for that.
Just try <dtml-call "desc.append(_[Category])"> <dtml-call "desc_qty.append(qty)">
You don't need the idx var at all this way.
You could also try a dictionary:
<dtml-call "REQUEST.set('desc', {})"> . . . <td> <dtml-var expr="_[Category]"> <dtml-if "_[Category][0:2] <> 'No'"> <dtml-call "desc[_[Category]] = qty"> </dtml-if> </td>
And you can later loop over the keys() or items() methods of the dictionary.
To set things straight: dictionaries are not allowed as variables in dtml - they result in syntax errors. In fact Martijn himself :-\ suggested alternatives for dictionaries in http://www.zope.org/pipermail/zope/1999-August/009166.html Rik
At 11:24 07/09/99 , Rik Hoekstra wrote:
To set things straight: dictionaries are not allowed as variables in dtml - they result in syntax errors. In fact Martijn himself :-\ suggested alternatives for dictionaries in http://www.zope.org/pipermail/zope/1999-August/009166.html
And straight again =): They are not supported as properties on a properties tab. That was what my post was about. In the post I actually create a dictionary in DTML and return it, as a solution to the lack of support in the property interface. But, you can't change a dictionary after creating it. The following works: <dtml-var standard_html_header> A little test: <dtml-call "REQUEST.set('dict', {'test': 1})"> <dtml-var "dict['test']"> <dtml-var standard_html_footer> But as soon as you want to add: <dtml-var "dict['test'] = 2"> Zope's parser throws a tantrum. Assignments are not allowed. I also tested the append method on lists: <dtml-var standard_html_header> A little test: <dtml-call "REQUEST.set('list', [])"> <dtml-call "list.append(1)"> <dtml-var "list[0]"> <dtml-var standard_html_footer> Works just fine. -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-7502100 Fax: +31-35-7502111 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
Martijn Pieters wrote:
And straight again =):
[snip]
But, you can't change a dictionary after creating it.
And one last teensy straightening <wink>. You can use the update method of dictionaries to change them, thus: <dtml-call "REQUEST.set('letsgo', {1: 'a one'})"> <dtml-call "letsgo.update({2: 'and a two'})"> <dtml-call "letsgo.update({(1,2,3,4): 'and a one, two, three, four!'})"> You cannot, AFAIK, delete keys from DTML. You can make a new copy of the dictionary with only selected keys, but you're better off in External Methods (soon PythonMethods!)
At 14:45 07/09/99 , Evan Simpson wrote:
Martijn Pieters wrote:
And straight again =):
[snip]
But, you can't change a dictionary after creating it.
And one last teensy straightening <wink>. You can use the update method of dictionaries to change them, thus:
<dtml-call "REQUEST.set('letsgo', {1: 'a one'})"> <dtml-call "letsgo.update({2: 'and a two'})"> <dtml-call "letsgo.update({(1,2,3,4): 'and a one, two, three, four!'})">
You cannot, AFAIK, delete keys from DTML. You can make a new copy of the dictionary with only selected keys, but you're better off in External Methods (soon PythonMethods!)
Aaarg. I knew I should've checked the Library Reference as well.. =) Evan, you're King. -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-7502100 Fax: +31-35-7502111 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
participants (5)
-
Evan Simpson -
Heiko Stoermer -
Martijn Pieters -
Rik Hoekstra -
Timothy Grant