相关疑难解决方法(0)

Jinja变量的范围是否可以扩展到内部块?

我有以下Jinja模板:

{% set mybool = False %}
{% for thing in things %}
    <div class='indent1'>
        <ul>
            {% if current_user %}
              {% if current_user.username == thing['created_by']['username'] %}
                {% set mybool = True %}
                <li>mybool: {{ mybool }}</li> <!-- prints True -->
                <li><a href='#'>Edit</a></li>
              {% endif %}
            {% endif %}
            <li>Flag</li>
        </ul>
    </div>
    <hr />
{% endfor %}

{% if not mybool %}
    <!-- always prints this -->
    <p>mybool is false!</p>
{% else %}
  <p>mybool is true!</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

如果在for …

variables templates scope jinja2

57
推荐指数
5
解决办法
4万
查看次数

标签 统计

jinja2 ×1

scope ×1

templates ×1

variables ×1