简体   繁体   中英

How to retrieve the value of a key in a dictionary using a variable defined in TAL?

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.

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.

DonCallisto的dic [var]确实有效

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM