Search code examples
twigdrupal-theming

Twig: How to use variable in range()


{% set lower_limit = 0 %}
{% set upper_limit = 10 %}
{% for i in range(lower_limit, upper_limit) %}
  ---
{% endfor %}

How should we use variables in range()? Above code doesn't work.


Solution

  • It was my mistake. Above code does work! Actually the variable which I was using in range was holding a string value. I used filter number_format. Now it works. Example...

    {% set dots = item.dots|number_format %}
    {% for i in range(1, dots) %}
      .
    {% endfor %}