I want to change the value of the variable declared outside the loop within a loop. But always changing, it keeps the initial value outside the loop.
我想要改变循环中循环外声明的变量的值。但是总是在变化,它将初始值保持在循环之外。
{% set foo = False %}
{% for item in items %}
{% set foo = True %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
这使得:
Ok(1)!
So the only (bad) solution have found so far was this:
到目前为止,唯一(糟糕的)解决方案是:
{% set foo = [] %}
{% for item in items %}
{% if foo.append(True) %} {% endif %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
这使得:
Ok(1)!
Ok(2)!
But, its is very ugly! Is there another more elegant solution?
但是,它很丑!还有更好的解决方案吗?
3 个解决方案
#1
49
Try also dictionary-based approach. It seems to be less ugly.
尝试也基于字典的方法。看起来不那么难看了。
{% set vars = {'foo': False} %}
{% for item in items %}
{% if vars.update({'foo': True}) %} {% endif %}
{% if vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if vars.foo %} Ok(2)! {% endif %}
This also renders:
这也使得:
Ok(1)!
Ok(2)!
#2
6
as mentioned in the documentation:
如文件所述:
Please note that assignments in loops will be cleared at the end of the iteration and cannot outlive the loop scope.
请注意循环中的赋值将在迭代结束时被清除,并且不能超过循环范围。
but as of version 2.10 you can use namespaces:
但是从2.10版本开始,您可以使用名称空间:
{% set ns = namespace(foo=false) %}
{% for item in items %}
{% set ns.foo = True %}
{% if ns.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if ns.foo %} Ok(2)! {% endif %}
#3
1
You could do this to clean up the template code
您可以这样做来清理模板代码
{% for item in items %}
{{ set_foo_is_true(local_vars) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
并在服务器代码中使用
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_foo_is_true(local_vars):
local_vars['foo'] = True
return ''
env.globals['set_foo_is_true'] = set_foo_is_true
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)
This could be generalized to the following
这可以概括为以下几点
{{ set_local_var(local_vars, "foo", False) }}
{% for item in items %}
{{ set_local_var(local_vars, "foo", True) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
并在服务器代码中使用
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_local_var(local_vars, name, value):
local_vars[name] = value
return ''
env.globals['set_local_var'] = set_local_var
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)
#1
49
Try also dictionary-based approach. It seems to be less ugly.
尝试也基于字典的方法。看起来不那么难看了。
{% set vars = {'foo': False} %}
{% for item in items %}
{% if vars.update({'foo': True}) %} {% endif %}
{% if vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if vars.foo %} Ok(2)! {% endif %}
This also renders:
这也使得:
Ok(1)!
Ok(2)!
#2
6
as mentioned in the documentation:
如文件所述:
Please note that assignments in loops will be cleared at the end of the iteration and cannot outlive the loop scope.
请注意循环中的赋值将在迭代结束时被清除,并且不能超过循环范围。
but as of version 2.10 you can use namespaces:
但是从2.10版本开始,您可以使用名称空间:
{% set ns = namespace(foo=false) %}
{% for item in items %}
{% set ns.foo = True %}
{% if ns.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if ns.foo %} Ok(2)! {% endif %}
#3
1
You could do this to clean up the template code
您可以这样做来清理模板代码
{% for item in items %}
{{ set_foo_is_true(local_vars) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
并在服务器代码中使用
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_foo_is_true(local_vars):
local_vars['foo'] = True
return ''
env.globals['set_foo_is_true'] = set_foo_is_true
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)
This could be generalized to the following
这可以概括为以下几点
{{ set_local_var(local_vars, "foo", False) }}
{% for item in items %}
{{ set_local_var(local_vars, "foo", True) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
并在服务器代码中使用
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_local_var(local_vars, name, value):
local_vars[name] = value
return ''
env.globals['set_local_var'] = set_local_var
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)