I read the docs and I am not clear on this is right at all. I know you can use nested for loops, but if statements seem to be different.
我看了医生的资料,我不清楚这是否正确。我知道可以使用嵌套的for循环,但是如果语句看起来不同的话。
Can i do the following?
我能做下面的事吗?
{% if thing=true %}
<div> something here</div>
{% if diffthing=true %}
<div> something else</div>
{% else %}
<div> third thing</div>
{% endif %}
{% else %}
<div> nothing here </div>
{% endif %}
Or should the format be different somehow?
或者格式应该有所不同?
3 个解决方案
#1
8
Jinja2 supports nested blocks, including if statements and other control structures.
Jinja2支持嵌套块,包括if语句和其他控制结构。
See the documentation on Block Nesting and Scope: "Blocks can be nested for more complex layouts."
请参阅有关块嵌套和范围的文档:“块可以嵌套用于更复杂的布局。”
A good use case for this is writing macros that conditionally output HTML:
一个好的用例是编写有条件输出HTML的宏:
{# A macro that generates a list of errors coming back from wtforms's validate function #}
{% macro form_error_summary(form, li_class='bg-danger') %}
{# only do the following on error... #}
{% if form.errors %}
<ul class="errors">
{# you can do layers of nesting as needed to render your content #}
{% for _field in form %}
{% if _field.errors %}
{% for error in _field.errors %}
<li class={{li_class}}>{{_field.label}}: {{ error|e }}</li>
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
#2
1
It seem possible. Refer to the documentation here: http://jinja.pocoo.org/docs/templates/#if
这看起来可能的。参考这里的文档:http://jinja.pocoo.org/docs/templates/#if
#3
1
The answer is yes.
答案是肯定的。
I'm using logic very similar to yours in a live application and the nested if blocks work as expected. It can get a little confusing if you don't keep your code clean, but it works fine.
我在动态应用程序中使用的逻辑与您的非常相似,嵌套的if块按预期工作。如果您不保持代码的整洁,它可能会有点混乱,但是它工作得很好。
#1
8
Jinja2 supports nested blocks, including if statements and other control structures.
Jinja2支持嵌套块,包括if语句和其他控制结构。
See the documentation on Block Nesting and Scope: "Blocks can be nested for more complex layouts."
请参阅有关块嵌套和范围的文档:“块可以嵌套用于更复杂的布局。”
A good use case for this is writing macros that conditionally output HTML:
一个好的用例是编写有条件输出HTML的宏:
{# A macro that generates a list of errors coming back from wtforms's validate function #}
{% macro form_error_summary(form, li_class='bg-danger') %}
{# only do the following on error... #}
{% if form.errors %}
<ul class="errors">
{# you can do layers of nesting as needed to render your content #}
{% for _field in form %}
{% if _field.errors %}
{% for error in _field.errors %}
<li class={{li_class}}>{{_field.label}}: {{ error|e }}</li>
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
#2
1
It seem possible. Refer to the documentation here: http://jinja.pocoo.org/docs/templates/#if
这看起来可能的。参考这里的文档:http://jinja.pocoo.org/docs/templates/#if
#3
1
The answer is yes.
答案是肯定的。
I'm using logic very similar to yours in a live application and the nested if blocks work as expected. It can get a little confusing if you don't keep your code clean, but it works fine.
我在动态应用程序中使用的逻辑与您的非常相似,嵌套的if块按预期工作。如果您不保持代码的整洁,它可能会有点混乱,但是它工作得很好。