[Zope] iterating dictionaries with dtml-in

Jonathan dev101 at magma.ca
Sat Sep 2 08:23:47 EDT 2006


----- Original Message ----- 
From: "Ferhat Ayaz" <ferhatayaz at yahoo.com>
To: <zope at zope.org>
Sent: Saturday, September 02, 2006 7:40 AM
Subject: [Zope] iterating dictionaries with dtml-in


> Hi,
> I know it's a silly question. But I was not able to
> find an example for this:
>
> I have a list of dicts like
> [
> {'name':'John','age':27},
> {'name':'Marc','age':17},
> {'name':'Susanne','age':19}
> ]
>
> Now I want to iterate each 'name' like
> <select>
> <option>John</option>
> <option>Marc</option>
> <option>Susanne</option>
> </select>
>
> I want to do this with DTML.
>
> <select>
> <dtml-in getDicts>
> <option>
>         HOW CAN I PUT the keys 'name' HERE ????
> </option>
> </dtml-in>
> </select>
> (getDicts returns the above list of dictionaries)

If getDicts returns a list of python dictionaries then you are going to 
require an outer loop (to run thru the list) and an inner loop (to run thru 
the keys of the current dictionary).  Something like this will get you going 
in the right direction:

<dtml-let dictList="getDicts">
    <dtml-in dictList prefix=aDict>
        <dtml-in "aDict_item.keys()">
            <dtml-var sequence-item>, <dtml-var 
"aDict_item[_['sequence-item']]">
        </dtml-in>
    </dtml-in>
 </dtml-let>


However, it would be much easier to change your getDicts routine to format 
the data you need for your OPTION statements, instead of extracting this 
information via dtml.

hth

Jonathan




More information about the Zope mailing list