views:

14

answers:

1

I have the following dictionary:

{0: {'Shortname': 'cabling', 'Name': 'CAT5 Cabling', 'MSRP': '$45.00'}, 1: {'Shortname': 'antenna', 'Name': 'Radio Antenna', 'MSRP': '$35.00'}}

And using Cheetah, the following section of template:

#for $item in $items
<tr>
<td>$item.Name</td>
<td>$item.MSRP</td>
</tr>
#end for

When I run the code, I get this error:

<class 'Cheetah.NameMapper.NotFound'>: cannot find 'Name'
      args = ("cannot find 'Name'",)
      message = "cannot find 'Name'" 

In the template, line 1, in the #for declaration, I have tried separating out the key value, such as:

#for $key, $value in $items

However, I still cannot iterate over the values to get the necessary information.

Am I doing something blatantly wrong?

A: 

First off, I had problems using items as the variable name in Cheetah. Might be a reserved word or something. Something less generic might be better to use.

Secondly, since you use an outer dict with an integer as key, $item will be that integer. Which means you look for Name in an integer. So in your case with a dict like that, you might do (with a different name of the dict too):

#for $item in $products
<td>$products[$item].Name</td>
...
#end for

A list like this would work with your template.

products = [{'Name':'prod1', ...}, {'Name': 'prod2', ...}]
plundra
That worked flawlessly! Thank you very much.I tried using an iterator with range (for $i in range(1, amountOfProducts), but that didn't seem to work either. Very tricky.In any case, you fixed it! Thank you so much for your help.
Eamon