Targeting every nth index from x to y in Twig

     

I'm having a problem with the loop.index in a for loop for a complex grid (marked up as a simple single level HTML list). I have a loop_index variable in a twig partial for a grid "cell" (a

  • ) which is equal to loop.index.


    Initially I had this simple specific numbers based check for the first items in the loop.


    {% set class = 'green' %}  {% if (loop_index > 1 and loop_index < 5) or (loop_index > 7 and loop_index < 12) %}   {% set class = 'orange' %}  {% endif %}  

    But now it should be extended to include possibly unlimited number of items in the loop. Therefore not only items 2-4 and 8-11 would become orange but also 15-18 etc.


    Basically I need to cycle through 2 classes (orange green) in this way:


        
    • 2-4 - orange
    •   
    • (repeating pattern begins from here:)
    •   
    • 5-7 - green (batch of 3)
    •   
    • 8-11 - orange (batch of 4)
    •   
    • 12-14 - green (batch of 3)
    •   
    • 15-18 - orange (batch of 4)
    •   
    • 19-21 - green (batch of 3)
    •   
    • 22-25 - orange (batch of 4)
    •   
    • ...
    •   

    I tried to employ batch and cycle for that