Mat*_*ris 57 variables templates scope jinja2
我有以下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 %}
如果在for循环中满足条件,我想mybool改为true,所以我可以mybool is true!在下面显示.但是,看起来内部的范围mybool仅限于if语句,因此永远不会设置所需的 mybool范围.
如何设置"全局",mybool以便我可以在最后一个if语句中使用它?
编辑
我发现了一些建议(只有正确的缓存页面视图),但它们似乎不起作用.也许他们在Jinja2中被弃用了......
编辑
解决方案如下.我仍然很好奇为什么上述建议不起作用.有谁知道他们被弃用了吗?
Gar*_*ett 47
解决此限制的一种方法是启用"do"表达式语句扩展并使用数组而不是布尔值:
{% set exists = [] %}
{% for i in range(5) %}
      {% if True %}
          {% do exists.append(1) %}
      {% endif %}
{% endfor %}
{% if exists %}
    <!-- exists is true -->
{% endif %}
要启用Jinja的"do"表达式语句扩展: e = jinja2.Environment(extensions=["jinja2.ext.do",])
God*_*ith 15
回答相关问题:我希望有一个全局计数器,表示我在模板中输入某个if-block的次数,最后得到了以下结果.
在模板的顶部:
{% set counter = ['1'] %}
在if-block中我想要计算:
{% if counter.append('1') %}{% endif %}
显示计数时:
{{ counter|length }}
'1'我相信,字符串可以替换为任何字符串或数字.它仍然是一个黑客,但不是一个非常大的.
你可以使用这个hack解决你的问题(没有扩展):
import jinja2
env = jinja2.Environment()
print env.from_string("""
{% set mybool = [False] %}
{% for thing in things %}
    <div class='indent1'>
        <ul>
            {% if current_user %}
              {% if current_user.username == thing['created_by']['username'] %}
                {% set _ = mybool.append(not mybool.pop()) %}
                <li>mybool: {{ mybool[0] }}</li> <!-- prints True -->
                <li><a href='#'>Edit</a></li>
              {% endif %}
            {% endif %}
            <li>Flag</li>
        </ul>
    </div>
    <hr />
{% endfor %}
{% if not mybool[0] %}
    <!-- always prints this -->
    <p>mybool is false!</p>
{% else %}
  <p>mybool is true!</p>
{% endif %}
""").render(current_user={'username':'me'},things=[{'created_by':{'username':'me'}},{'created_by':{'username':'you'}}])
这是任何想要使用namespace()对象在for循环外持久化变量的人的一般情况。
{% set accumulator = namespace(total=0) %}
{% for i in range(0,3) %}
    {% set accumulator.total = i + accumulator.total %}
    {{accumulator.total}}
 {% endfor %}`          {# 0 1 3 #}
 {{accumulator.total}}  {# 3 (accumulator.total persisted past the end of the loop) #}
| 归档时间: | 
 | 
| 查看次数: | 42312 次 | 
| 最近记录: |