Answers.Body: <p>In the second inner loop, you need to add the number of items in the first panel to the current index to get a consistent count.</p>
  <p>Might be an off-by-one-error in the following example, make sure to adjust for your desired output …</p>
  <pre class="lang-twig prettyprint-override"><code>{% set panel_one_items = block.nt_panel_one.all() %}
  {% set panel_two_items = block.nt_panel_two.all() %}
  {% for item in panel_one_items %}
      {# … #}
  {% endfor %}
  
  {% for item in panel_two_items %}
      {% set is_even = ((panel_one_items|length) + loop.index0) is even %}
      {% … %}
  {% endfor %}
  </code></pre>
  <hr />
  <p>As a sidenote: With modern CSS, you probably don't need to determine if the current item is even or odd at all. If you need to switch some styling (like alignment) based on that, you can use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child" rel="nofollow noreferrer"><code>:nth-child</code> selector</a> instead. Your code looks like its for some kind of grid layout with rows and columns? With <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout" rel="nofollow noreferrer">CSS Grid</a>, you don't need rows at all and get nice layout with proper spacing, alignment and gaps by default.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T14:22:01.813Z
Answers.Text: In your first example, I get displayed two odds and then an even. Do I need to adjust somehow?
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T14:28:44.070Z
Answers.Text: @DanLee Yeah, set `last_element_is_even` to `true` initially instead of `false`. First is one, so first should be odd … fixed in my example code.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T14:34:19.657Z
Answers.Text: Ok now I get one odd and two evens...
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T14:48:06.877Z
Answers.Text: See updated code and image showing the resault. Any idea what might be the issue?
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T15:00:40.977Z
Answers.Text: @DanLee Ok my bad, first approach doesn't work at all – you need the offset for each iteration in the second loop. I've updated my answer to only include the second approach, this one should work and is a bit more reliable. Keep in mind that code examples are only ever that – never copy them directly, and always try to understand what they're doing. This way, you can spot and correct error like this and aren't relying on a stranger on the internet for business logic :) Also, maybe reconsider the caveat below the line.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T15:04:44.693Z
Answers.Text: @DanLee Example code should work now, here's a fiddle for reference: https://twigfiddle.com/rpgwpt
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T15:15:50.237Z
Answers.Text: Thanks for the example - I understand how that works - it counts the items in each loop... and it references the first loop in the second. My issue is that the loops will be reversible. So loop two could come before loop one. Do you have any thoughts on how this could be done?
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T15:37:30.160Z
Answers.Text: @DanLee Your template hardcodes the position, how would that be reversible? If you have some sort of switch in your template, use that to determine which panels have been included already and count the number of blocks in them. Again, I'm pretty sure this can be solved much more elegantly with CSS grid.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T09:08:25.460Z
Answers.LastActivityDate: 2022-01-13T14:58:24.820Z
Answers.LastEditDate: 2022-01-13T14:58:24.820Z
Answers.UserDisplayName: MoritzLost
Body: <p>I have a parent forloop that cycles through my content blocks and multiple child forloops that cycle through list items. I'm trying to alternate the left and right alignment for my child forloop items and for it to carry through into the sibling child forloops.</p>
  <p>How do I define and carry through the previous alignment so the sibling child forloops can start off with the correct alignment class?</p>
  <p>This is what I had initally:</p>
  <pre><code>{% for block in entry.nt_content.level(1).all() %}
  
      {% for item in block.nt_panel_one.all() %}
        {% if (loop.index is even) %}
          {% set _rows = 'tw-row-start-1' %}
        {% else %}
          {% set _rows = '' %}
        {% endif %}
      {% endfor %}
  
      {% for item in block.nt_panel_two.all() %}
        {% if (loop.index is even) %}
          {% set _rows = 'tw-row-start-1' %}
        {% else %}
          {% set _rows = '' %}
        {% endif %}
      {% endfor %}
  
  {% endfor %}
  </code></pre>
  <p><strong>Update</strong></p>
  <p>Updated code based on Intermediate variable method showing code and visual representation.</p>
  <p><a href="https://i.sstatic.net/wVeEQ.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wVeEQ.jpg" alt="enter image description here" /></a></p>
  <pre><code>{% for block in entry.nt_content.level(1).all() %}
  
  {% set last_element_is_even = true %}
  
  {% for item in block.nt_panel_one.all() %}
      {% if last_element_is_even %}
          {% set _rows = 'tw-row-start-1' %}
      {% else %}
          {% set _rows = '' %}
      {% endif %}
  
      {% set last_element_is_even = loop.index is even %}
  {% endfor %}
  
  
  {% for item in block.nt_panel_two.all() %}
      {% if last_element_is_even %}
          {% set _rows = 'tw-row-start-1' %}
      {% else %}
          {% set _rows = '' %}
      {% endif %}
  
      {% set last_element_is_even = loop.index is even %}
  {% endfor %}
  {% endfor %}
  </code></pre>
  
Tags: <forloop>
Title: Alternating Forloop items across Content Blocks
ThreadText: # Alternating Forloop items across Content Blocks
  
  (Asked by: username_0 on 2022-01-12)
  
  I have a parent forloop that cycles through my content blocks and multiple child forloops that cycle through list items. I'm trying to alternate the left and right alignment for my child forloop items and for it to carry through into the sibling child forloops.
  
  How do I define and carry through the previous alignment so the sibling child forloops can start off with the correct alignment class?
  
  This is what I had initally:
  
  ```
  {% for block in entry.nt_content.level(1).all() %}
  
      {% for item in block.nt_panel_one.all() %}
        {% if (loop.index is even) %}
          {% set _rows = 'tw-row-start-1' %}
        {% else %}
          {% set _rows = '' %}
        {% endif %}
      {% endfor %}
  
      {% for item in block.nt_panel_two.all() %}
        {% if (loop.index is even) %}
          {% set _rows = 'tw-row-start-1' %}
        {% else %}
          {% set _rows = '' %}
        {% endif %}
      {% endfor %}
  
  {% endfor %}
  
  ```
  
  **Update**
  
  Updated code based on Intermediate variable method showing code and visual representation.
  
  [![enter image description here](https://i.sstatic.net/wVeEQ.jpg)](https://i.sstatic.net/wVeEQ.jpg)
  
  ```
  {% for block in entry.nt_content.level(1).all() %}
  
  {% set last_element_is_even = true %}
  
  {% for item in block.nt_panel_one.all() %}
      {% if last_element_is_even %}
          {% set _rows = 'tw-row-start-1' %}
      {% else %}
          {% set _rows = '' %}
      {% endif %}
  
      {% set last_element_is_even = loop.index is even %}
  {% endfor %}
  
  {% for item in block.nt_panel_two.all() %}
      {% if last_element_is_even %}
          {% set _rows = 'tw-row-start-1' %}
      {% else %}
          {% set _rows = '' %}
      {% endif %}
  
      {% set last_element_is_even = loop.index is even %}
  {% endfor %}
  {% endfor %}
  
  ```
  
  **Tags:** forloop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-01-13. Score: 3
  
  In the second inner loop, you need to add the number of items in the first panel to the current index to get a consistent count.
  
  Might be an off-by-one-error in the following example, make sure to adjust for your desired output …
  
  ```twig
  {% set panel_one_items = block.nt_panel_one.all() %}
  {% set panel_two_items = block.nt_panel_two.all() %}
  {% for item in panel_one_items %}
      {# … #}
  {% endfor %}
  
  {% for item in panel_two_items %}
      {% set is_even = ((panel_one_items|length) + loop.index0) is even %}
      {% … %}
  {% endfor %}
  
  ```
  
  ---
  
  As a sidenote: With modern CSS, you probably don't need to determine if the current item is even or odd at all. If you need to switch some styling (like alignment) based on that, you can use the [`:nth-child` selector](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child) instead. Your code looks like its for some kind of grid layout with rows and columns? With [CSS Grid](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout), you don't need rows at all and get nice layout with proper spacing, alignment and gaps by default.
  
  > **Comments:**
  > **username_0**: In your first example, I get displayed two odds and then an even. Do I need to adjust somehow?  
  > **username_1**: @username_0 Yeah, set `last_element_is_even` to `true` initially instead of `false`. First is one, so first should be odd … fixed in my example code.  
  > **username_0**: Ok now I get one odd and two evens...  
  > **username_0**: See updated code and image showing the resault. Any idea what might be the issue?  
  > **username_1**: @username_0 Ok my bad, first approach doesn't work at all – you need the offset for each iteration in the second loop. I've updated my answer to only include the second approach, this one should work and is a bit more reliable. Keep in mind that code examples are only ever that – never copy them directly, and always try to understand what they're doing. This way, you can spot and correct error like this and aren't relying on a stranger on the internet for business logic :) Also, maybe reconsider the caveat below the line.  
  > **username_1**: @username_0 Example code should work now, here's a fiddle for reference: https://twigfiddle.com/rpgwpt  
  > **username_0**: Thanks for the example - I understand how that works - it counts the items in each loop... and it references the first loop in the second. My issue is that the loops will be reversible. So loop two could come before loop one. Do you have any thoughts on how this could be done?  
  > **username_1**: @username_0 Your template hardcodes the position, how would that be reversible? If you have some sort of switch in your template, use that to determine which panels have been included already and count the number of blocks in them. Again, I'm pretty sure this can be solved much more elegantly with CSS grid.  
  
  ---
  
  