-2

Hello I would like do something like that with my Twig template

<?php
    for( $i = 0; $i <= 5; $i++ ) {
        // Not display the first number
        if( $i <= 1 ) {
            continue;
        }
        // Displaying numbers from 2 to 5
        echo $i ,'<br/>';
    }
?>

How can I do that ?

Thanks for your help.

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49

2 Answers2

1

From the documentation you can use this to iterate numbers

{% for i in 0..10 %}
    * {{ i }}
{% endfor %} 

Also from the documentation you can add conditions like this

<ul>
    {% for user in users if user.active %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
</ul>

So if you combine the two you end up with something like this.

{% for i in 0..5 if i<= 1 %}
    * {{ i }}
{% endfor %} 

Untested but should work. THe documentation : http://twig.sensiolabs.org/doc/tags/for.html

Theodore Enderby
  • 622
  • 1
  • 6
  • 19
0

If you literally just want to skip the first iteration, you can just do

{% for i in 1..5 %}

or

{% for i in 0..5 if loop.index0 %}

But assuming you want to actually do something more useful like some processing on the first iteration, then just only echo $i on subsequent iterations, this should work:

{% for i in 0..5 %}
    This is printed every time...
    {% if (loop.index0) %}
        ...but this is only printed when $i > 0: {{ i }}<br />
    {% endif %}
{% endfor %}

There's not a "continue" keyword or any equivalent in Twig as far as I know.

Joe
  • 15,669
  • 4
  • 48
  • 83