dongmian5325 2012-03-08 04:28
浏览 224
已采纳

Twig PHP:在Foreach中增加和减去一个值

I am working on a project that loops 100 times with repeating data. Every 8th post I am inserting an advertisement block, because I am using the index value to number each block output I need to subtract from a variable because the ad block isn't numbered. So the issue I have now is the following.

Block #1

Block #2

Block #3

Block #4

Block #5

Block #6

Block #7

Advertisement Block

Block #9

Because it is counting the advertisement block as one iteration of the index, the block that follows it which will have a number is now 9 when it should be 8. Is there a way to increment a variable and then subtract a value of 1 from it every time an advertisement block shows?

In standard PHP I could do this easy, but with Twig I've tried a few things and am at a loss as to what I can do.

  • 写回答

1条回答 默认 最新

  • douzhang1299 2012-03-08 05:42
    关注

    If i understandly 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 %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器