烧瓶模板的子串

时间:2015-05-17 17:11:40

标签: python flask

我有一个循环来显示烧瓶模板中的列表内容但是我不想显示元素的第一个字符,这样就可以在python中工作但不在烧瓶中

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

我收到此错误

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

2 个答案:

答案 0 :(得分:5)

如果您希望以这种方式使用变量,则需要set个变量。 (Documentation)。

那就是说 - 你应该能够在{{ file['path'][1:] }}循环中for。{/ p>

答案 1 :(得分:3)

重复this question

您必须使用{% set %}模板标记在jinja2模板中分配变量:

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