Twig PHP: Increase and Subtract Value in Foreach

I am working on a project that repeats 100 times with duplicate data. Every eighth post I insert an ad unit because I am using the index value for the number of each output unit, which needs to be subtracted from the variable, because the ad unit is not numbered. So, the problem I am currently having is the following:

Block #1

Block #2

Block #3

Block #4

Block #5

Block #6

Block #7

Advertisement Block

Block #9

      

Since it counts the ad unit as one iteration of the index, the subsequent block that will have a number is now 9 when it should be 8. Is there any way to increase the value of the variable and then subtract the value 1 from it every time the ad is displayed block?

In standard PHP I could do this easily, but with Twig I have tried a few things and am not sure what I can do.

+3


source to share


1 answer


If I understand correctly, you can do this:



{% for foo in bar%}
  {% if (loop.index% 8 == 0 and loop.index> 0)%}
    {# You advertisement here #}
  {% endif%}
  {# Your standard block here #}
  <p> This is block # {{loop.index + 1 + loop.index // 8}} </p>
{% endfor%}
+4


source







All Articles