我有以下模板:
{% set rotator = 1 %}
{% for idx in range(1, count|int + 1) %}
{% if rotator == 4 %}
{% set rotator = 1 %}
{% endif %}
{
"id": "{{ '%02d' % idx }}",
"value": "{{rotator}}"
},
{% set rotator = rotator + 1 %}
{% endfor %}
由于此处讨论的问题How to increment a variable on a for loop in jinja template?,此模板无效
对于doesn't work
我的意思是旋转器总是一个并且不会改变。
我怎样才能克服以下问题?
答案 0 :(得分:1)
模板:
{% for idx in range(1, count|int + 1) %}
{
"id": "{{ '%02d' % idx }}",
"value": "{{ (idx+2)%3+1 }}"
},
{% endfor %}
结果(count=7
):
{
"id": "01",
"value": "1"
},
{
"id": "02",
"value": "2"
},
{
"id": "03",
"value": "3"
},
{
"id": "04",
"value": "1"
},
{
"id": "05",
"value": "2"
},
{
"id": "06",
"value": "3"
},
{
"id": "07",
"value": "1"
},
我留下了结尾,
,因为你没有说明如何处理它。