[英]'if' statement in jinja2 template
I'm trying to write an if statement in jinja template :我正在尝试在jinja 模板中编写一个 if 语句:
{% for key in data %}
{% if key is 'priority' %}
<p>('Priority: ' + str(data[key])</p>
{% endif %}
{% endfor %}
the statement I'm trying to translate in Python is:我试图用 Python 翻译的语句是:
if key == priority:
print(print('Priority: ' + str(data[key]))
This is the error i'm getting:这是我得到的错误:
TemplateSyntaxError: expected token 'name', got 'string'
TemplateSyntaxError:预期标记“名称”,得到“字符串”
Why the loop?为什么是循环?
You could simply do this:你可以简单地这样做:
{% if 'priority' in data %}
<p>Priority: {{ data['priority'] }}</p>
{% endif %}
When you were originally doing your string comparison, you should have used ==
instead.当您最初进行字符串比较时,您应该使用
==
代替。
We need to remember that the {% endif %}
comes after the {% else %}
.我们需要记住
{% endif %}
出现在{% else %}
。
So this is an example:所以这是一个例子:
{% if someTest %}
<p> Something is True </p>
{% else %}
<p> Something is False </p>
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.