Search code examples
nunjucks

test in Nunjucks if a dict has key


Some of the test data have img some not.

I am new to Nunjucks and google does not find how to test if the record have key img?

I try the following:

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

But seems does not work.

I have also search the Nunjucks doc, but does not find has_key or some other info.

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

Solution

  • {% if r.img is defined %} should also do the trick. – Lesha Ogonkov 3 hours ago