How to retrieve the value of a key in a dictionary using a variable defined in TAL?
-
14-04-2021 - |
Question
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.
Solution 2
DonCallisto's dic[var] does work
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow