Jinja模板块中的嵌套块和for循环 [英] nested block and for loop in a Jinja template block
本文介绍了Jinja模板块中的嵌套块和for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试在Jinja模板块设置中使用嵌套块和for循环.
Trying to use nested block and for loop in a Jinja template block setup.
{% block main %}
<table>
<tr>
<td>user id</td>
<td>user sec level</td>
</tr>
{% block main_nested_b scoped %}
{%
for user in list_users:
t_id_user = str(user[0][0])
t_sec_level = str(user[2][0])
%}
<tr>
<td>
<a href='/usersEdit?id_user={{ t_id_user }}' class='onwhite'>edit</a>
</td>
</tr>
{% endfor %}
{% endblock main_nested_b %}
{% endblock main %}
</table>
错误消息:
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 't_id_user'
帮助?
推荐答案
您不能将Jinja语法视为Python语法.这不是同一回事.将您的 for
标记与
You can't treat Jinja syntax as Python syntax. It's not the same thing. Keep your for
tag separate from assignment (set
) tags:
{% for user in list_users %}
{% set t_id_user = user[0][0] %}
{% set t_sec_level = user[2][0] %}
请注意,for ... in ...
语法的末尾甚至没有:
!另外,您无需在此处调用str()
,将其留给Jinja即可为您转换为字符串.在任何使用{{ t_id_user }}
或{{ t_sec_level }}
的位置,该值将始终转换为字符串.
Note that there isn't even a :
at the end of the for ... in ...
syntax! Also you don't need to call str()
here, leave that to Jinja to convert to strings for you; anywhere you use {{ t_id_user }}
or {{ t_sec_level }}
the value will be converted to a string anyway.
这是完整的模板:
<table>
{% block main %}
{% block main_nested_b scoped %}
{% for user in list_users %}
{% set t_id_user = user[0][0] %}
{% set t_sec_level = user[2][0] %}
<tr>
<td>
<a href='/usersEdit?id_user={{ t_id_user }}' class='onwhite'>edit</a>
</td>
</tr>
{% endfor %}
{% endblock main_nested_b %}
{% endblock main %}
</table>
这篇关于Jinja模板块中的嵌套块和for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文