loopsfor-looptwig

Twig - How to loop particular number of times


I need to be able to generate the links certain number of times (stored in int variable)Is there a way to do it out of the box with twig's for loop?

{% for i in numberOfLoops %}
    {{ i }}. Some data
{% endfor %}

The above example do not work. I googled it but did not find actual solution. Any support would be very appreciated.

EDIT: I also tried:

{% set k = 10 %}
{% for i in 0..k %}
    {{ i }}
{% endfor %}

but this generates an exception:

com.lyncode.jtwig.exception.ParseException: Wrong binary operation syntax
Explanation: Input position (line 15, pos 27):
        {% for i in 0..k %}
                      ^

Solution

  • I found the working example:

    {% set k = 10 %}
    {% for i in range(1, k) %}
        {{ i }}
    {% endfor %}
    

    Source: http://twig.sensiolabs.org/doc/templates.html (not very intuitive to find indeed).