8

我有我像这样传递给模板的字典

dict['vars'] = ['a':'dd','b':'gg','c':'yy']
dict['myvars'] = ['a':'tt','b':'yy','c':'uu']

有时

当单个字典为空且没有键时。

我怎么能像这样检查

{%  if dict.vars empty   %}
do this
4

4 回答 4

18
{% if not dict.vars %}
  do this
{% endif %}
于 2013-04-22T06:44:48.760 回答
9

这:

{% if dict|length > 0 %} 
 do this
{% endif %}

也应该做得很好。

于 2018-01-24T01:31:54.623 回答
3

你也可以这样做:

{% if not "vars" in dict %}
    Hello
{% else %}
    abc
{% endif %}
于 2013-04-22T07:11:39.843 回答
0

您也可以像这样检查字典是否为空

{% if not 'bool(video_url_dict)' %}
{% if 'bool(video_url_dict)' %}

这将正常工作。

于 2020-10-31T16:14:26.843 回答