Flask Jinja if语句执行不正确

Flask Jinja if语句执行不正确,flask,Flask,我正在使用Jinja+烧瓶 下面是示例代码: # Flask @app.route('/test/<int:counter>'): return render_templates('test.html', counter) # Jinja ## test.html {% extends "layout.html" %} {% if counter|int == 1 %} {% block scoreboard1 %}active1{% endblock %} {% elif

我正在使用Jinja+烧瓶

下面是示例代码:

# Flask
@app.route('/test/<int:counter>'):
    return render_templates('test.html', counter)

# Jinja
## test.html
{% extends "layout.html" %}
{% if counter|int == 1 %}
{% block scoreboard1 %}active1{% endblock %}
{% elif counter|int == 2 %}
{% block scoreboard2 %}active2{% endblock %}
{% elif counter|int == 3 %}
{% block scoreboard3 %}active3{% endblock %}
{% else %}
{% endif %}

## layout.html
<a href="/scoreboard/1" class="{% block scoreboard1 %}{% endblock %} item">1</a>
<a href="/scoreboard/2" class="{% block scoreboard2 %}{% endblock %} item">2</a>
<a href="/scoreboard/3" class="{% block scoreboard3 %}{% endblock %} item">3</a>
#烧瓶
@应用程序路径('/test/'):
返回呈现模板('test.html',计数器)
#金贾
##test.html
{%extends“layout.html”%}
{%if计数器| int==1%}
{%block记分板1%}active1{%endblock%}
{%elif计数器| int==2%}
{%block记分板2%}active2{%endblock%}
{%elif计数器| int==3%}
{%block记分板3%}active3{%endblock%}
{%else%}
{%endif%}
##layout.html
以下是结果(在chrome开发工具中检查):

#URL->127.0.0.1/test/2
预期结果:

# URL -> 127.0.0.1/test/2
<a href="/scoreboard/1" class=" item">1</a>
<a href="/scoreboard/2" class="active2 item">2</a>
<a href="/scoreboard/3" class=" item">3</a>
#URL->127.0.0.1/test/2
我希望block将取决于计数器值,但它会完全改变它


我有点好奇是什么原因触发了这个bug,你不能在这样的条件下使用
,不管条件是否满足,只要你的
在你的子模板中,它就会被填充

有两种方法可以实现(我假设是)你想要的,例如,你可以使用:

但是,如果您想使用现有的结构
layout.html
,当然可以稍微调整并将条件放入块中:

{% block scoreboard1 %}
    {% if counter|int == 1 %}active1{% endif %}
{% endblock %}

您不能在这样的条件中使用
,无论是否满足条件,只要
在子模板中,它就会被填充

有两种方法可以实现(我假设是)你想要的,例如,你可以使用:

但是,如果您想使用现有的结构
layout.html
,当然可以稍微调整并将条件放入块中:

{% block scoreboard1 %}
    {% if counter|int == 1 %}active1{% endif %}
{% endblock %}
这解决了我的问题:))。刚刚发现我有多笨:p这解决了我的问题:))。刚刚发现我有多笨:P
...
{{ scoreboardlink(counter) }}
...
{% block scoreboard1 %}
    {% if counter|int == 1 %}active1{% endif %}
{% endblock %}