AFS
AFS

Reputation: 1443

Substring at flask template

I have a loop to show a list content in a flask template but I don't want to show the first character of the element , in this way works in python but not in flask

{%for file in files%}
        {% f= file['path'] %}
        <p> {{ f[1:] }}</p>
{% endfor %}

I get this error

TemplateSyntaxError: Encountered unknown tag 'f'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.

Upvotes: 2

Views: 3897

Answers (2)

Michael Blakeley
Michael Blakeley

Reputation: 346

Duplicate of this question.

You have to use the {% set %} template tag to assign variables within a jinja2 template:

{% for file in files %}
    {% set f = file['path'] %}
    <p>{{ f[1:] }}</p>
{% endfor %}

Upvotes: 3

Doobeh
Doobeh

Reputation: 9450

You need to set variables if you wish to use them in that way. (Documentation).

That said— you should be able to just do {{ file['path'][1:] }} within your for loop.

Upvotes: 6

Related Questions