在 Nunjucks 中测试字典是否有键

test in Nunjucks if a dict has key

测试数据有的有img有的没有

我是 Nunjucks 的新手,google 没有找到如何测试记录是否有密钥 img?

我尝试以下操作:

{% for r in testdata %}
  {% if r.img %} <img src="{{img}}" alt="{{r.heading}}">{% endif %}
  ....
{% endfor $}

不过好像不行。

我也搜索了 Nunjucks 文档,但没有找到 has_key 或其他一些信息。

testdata:
-
   type: G
   TID: 567
   heading: 泰國
   alt1: Tailânia
   source: 
-
   type: P
   TID: 4
   heading: Monteiro, Vicente
   alt1: 韋思明
   alt2: Monteiro, Vicente Joao
   source: 物業登記局局長 (1998)
-
   type: P
   TID: 1
   heading: Pires, Candida da Silva Antunes
   alt1: 李淑華
   source: 澳門大學法學院副教授, (1995)
   img: P0001.jpg

{% if r.img is defined %} 也应该可以解决问题。 – Lesha Ogonkov 3 小时前