简体   繁体   English

如何使用TAL中定义的变量在字典中检索键的值?

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

I have defined a dictionary in python: 我在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'. 我有另一个使用tal:define变量,称为var,它是'a'或'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}' . (然后foo为'1'或'2'),我得到KeyError: '${var}' I've tried dic[$var] , dic[var] , etc and they all don't work. 我试过dic[$var]dic[var]等,但它们都不起作用。 How do I use a variable defined previously using tal:define as a key to a dictionary to get its value. 如何使用先前使用tal:define的变量作为字典的键来获取其值。

Path expression syntax: 路径表达式语法:

tal:define="foo dic/?var"

Python expression syntax: Python表达式语法:

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

See Zope Book for description of ?var syntax. 有关?var语法的说明,请参见Zope书

DonCallisto的dic [var]确实有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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