[Zope] Building a list programmatically under DTML

Martijn Pieters mj@antraciet.nl
Tue, 07 Sep 1999 10:52:17 +0200


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
------------------------------------------