ScotterMonkey
ScotterMonkey

Reputation: 1044

nested block and for loop in a Jinja template block

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>

Error message:

jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 't_id_user'

Help?

Upvotes: 0

Views: 1229

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121584

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] %}

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.

Here is the complete template:

<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>

Upvotes: 2

Related Questions