{# **DOES NOT WORK AS INTENDED** #}
{% set prev = 0 %}
{% for x in [1, 2, 3, 5] %}
{%- if prev != x - 1 %}⋮ (prev was \{\{ prev }})
{% endif -%}
\{\{ x }}
{%- set prev = x %}
{% endfor %}
打印
1
⋮ (prev was 0)
2
⋮ (prev was 0)
3
⋮ (prev was 0)
5
因为变量没有被持久化。相反,你可以使用可变的命名空间包装:
{% set ns = namespace(prev=0) %}
{% for x in [1, 2, 3, 5] %}
{%- if ns.prev != x - 1 %}⋮ (ns.prev was \{\{ ns.prev }})
{% endif -%}
\{\{ x }}
{%- set ns.prev = x %}
{% endfor %}