0

I taken for in which I can't use loop index due to some reasons.I want to use something like {{set i=0}} and using modulo{%} with that I variable.in jinja template.

I used

{{Set i=1}}
{% for item in items %}
{% if I % 2==0%}
Print("hello")
{% endif%}
{% set i=i+1 %}
{% endfor%}

It's not working

  • 1
    `{{loop.index}}` would return the index. see https://stackoverflow.com/a/12147197/2681662, https://stackoverflow.com/a/7537466/2681662 – MSH Apr 10 '23 at 07:46
  • @MSH "I can't use loop index due to some reasons", the OP says, as weird as those unknown reasons may be. – blhsing Apr 13 '23 at 08:08

1 Answers1

1

You can create a namespace variable to allow access to the variable from an inner scope created by a for loop:

{{ set ns = namespace(i=1)}}
{% for item in items %}
{% if ns.i % 2 == 0%}
Print("hello")
{% endif %}
{% set ns.i = ns.i + 1 %}
{% endfor %}
blhsing
  • 91,368
  • 6
  • 71
  • 106