2

I have defined a dictionary in python:

tal:define="dic python:{'a':'1', 'b':'2'};"

I have another variable defined using tal:define called var, which is either 'a' or 'b'. I get an error when I try to do:

tal:define="foo python:dic['${var}'];"

(foo would then be '1' or '2') I get KeyError: '${var}'. I've tried dic[$var], dic[var], etc and they all don't work. How do I use a variable defined previously using tal:define as a key to a dictionary to get its value.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
JCarter
  • 407
  • 3
  • 11
  • 2
    dic[var] should work, are you sure you define correctly 'var' ... can i see the 'var' definition ? – DonCallisto Jan 04 '12 at 20:28
  • oh, geez. you're right, I remembered I tried that one and it didn't work so I've been trying some with the '$' and curly bracket combos. – JCarter Jan 04 '12 at 20:34

2 Answers2

5

Path expression syntax:

tal:define="foo dic/?var"

Python expression syntax:

tal:define="foo python:dic[var]"

See Zope Book for description of ?var syntax.

Radim Novotny
  • 323
  • 1
  • 8
0

DonCallisto's dic[var] does work

JCarter
  • 407
  • 3
  • 11