Answers.Body: <p>I don't think you can test a conditional against a batch set like this. I think it's testing each batched set against the conditional instead of each item inside the batched sets which is what you're going for.</p>
  
  <p>I would just create a new set of filtered matrix blocks and then batch the result.</p>
  
  <pre><code>{% set onlyHalfWidthBlocks = [] %}
  
  {% for block in entry.curatedContentBlocks if block.type == 'halfWidthBlock' %}
    {% set onlyHalfWidthBlocks = onlyHalfWidthBlocks|merge([block]) %}
  {% endfor %}
  
  {% for row in onlyHalfWidthBlocks|batch(2) %}
    &lt;div class="wrapper"&gt;
      &lt;div class="row"&gt;
        {% for block in row %}
          {# ... block code here ... #}
        {% endfor %}
      &lt;/div&gt; &lt;!-- .row --&gt;
    &lt;/div&gt; &lt;!-- .wrapper --&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-01T06:00:08.687Z
Answers.Text: Hey Sean, did this answer help?
Answers.UserDisplayName: Alex Roper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T19:22:15.940Z
Answers.LastActivityDate: 2018-03-21T19:22:15.940Z
Answers.UserDisplayName: Alex Roper
Body: <p>I am trying to make a grid of blocks using Twig. I have different types of blocks so I am using a conditional to test the block type and render different components per block type.</p>
  
  <p>The problem I am having is I want to batch my blocks (columns) in twos so that I can have two blocks per row, so I can do <code>{% for row in entry.curatedContentBlocks|batch(2) if row.type == 'halfWidthBlock' %}</code>, add a row and then nest <code>{% for block in row %}</code> and add my block styles. This should work but when I try to test the block type with <code>row.type</code>, I get an error saying <code>Key "type" for array with keys "0, 1" does not exist.</code>. I'm thinking this is something to do with the <code>batch</code> filter returning an array.</p>
  
  <p>I think the following code is the closest I've gotten, but I am still getting an error saying <code>Key "1" for array with keys "2, 3" does not exist.</code>. Here's the code:</p>
  
  <pre><code>{% for i, row in entry.curatedContentBlocks|batch(2) if row[i].type == 'halfWidthBlock' %}
    &lt;div class="wrapper"&gt;
      &lt;div class="row"&gt;
        {% for block in row %}
          &lt;div class="text-center c6 med-c12 push-top-double"&gt;
            &lt;div class="section fill-grey-light rounded"&gt;
              &lt;div class="c12 grid-center"&gt;
                &lt;h3&gt;{{ block.titleText }}&lt;/h3&gt;
                {% if block.ctaUrl|length and block.ctaText|length %}
                  &lt;a href="{{ block.ctaUrl }}" class="btn push-top"&gt;
                    {{ block.ctaText }}
                  &lt;/a&gt;
                {% endif %}
              &lt;/div&gt; &lt;!-- .c --&gt;
            &lt;/div&gt; &lt;!-- .section --&gt;
          &lt;/div&gt; &lt;!-- .c --&gt;
        {% endfor %}
      &lt;/div&gt; &lt;!-- .row --&gt;
    &lt;/div&gt; &lt;!-- .wrapper --&gt;
  {% endfor %}
  </code></pre>
  
Tags: <templating><forloop><loop><batch>
Title: Use Twig's batch filter with conditional
ThreadText: # Use Twig's batch filter with conditional
  
  (Asked by: username_0 on 2018-03-21)
  
  I am trying to make a grid of blocks using Twig. I have different types of blocks so I am using a conditional to test the block type and render different components per block type.
  
  The problem I am having is I want to batch my blocks (columns) in twos so that I can have two blocks per row, so I can do `{% for row in entry.curatedContentBlocks|batch(2) if row.type == 'halfWidthBlock' %}`, add a row and then nest `{% for block in row %}` and add my block styles. This should work but when I try to test the block type with `row.type`, I get an error saying `Key "type" for array with keys "0, 1" does not exist.`. I'm thinking this is something to do with the `batch` filter returning an array.
  
  I think the following code is the closest I've gotten, but I am still getting an error saying `Key "1" for array with keys "2, 3" does not exist.`. Here's the code:
  
  ```
  {% for i, row in entry.curatedContentBlocks|batch(2) if row[i].type == 'halfWidthBlock' %}
    <div class="wrapper">
      <div class="row">
        {% for block in row %}
          <div class="text-center c6 med-c12 push-top-double">
            <div class="section fill-grey-light rounded">
              <div class="c12 grid-center">
                <h3>{{ block.titleText }}</h3>
                {% if block.ctaUrl|length and block.ctaText|length %}
                  <a href="{{ block.ctaUrl }}" class="btn push-top">
                    {{ block.ctaText }}
                  </a>
                {% endif %}
              </div> <!-- .c -->
            </div> <!-- .section -->
          </div> <!-- .c -->
        {% endfor %}
      </div> <!-- .row -->
    </div> <!-- .wrapper -->
  {% endfor %}
  
  ```
  
  **Tags:** templating, forloop, loop, batch
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-21. Score: 1
  
  I don't think you can test a conditional against a batch set like this. I think it's testing each batched set against the conditional instead of each item inside the batched sets which is what you're going for.
  
  I would just create a new set of filtered matrix blocks and then batch the result.
  
  ```
  {% set onlyHalfWidthBlocks = [] %}
  
  {% for block in entry.curatedContentBlocks if block.type == 'halfWidthBlock' %}
    {% set onlyHalfWidthBlocks = onlyHalfWidthBlocks|merge([block]) %}
  {% endfor %}
  
  {% for row in onlyHalfWidthBlocks|batch(2) %}
    <div class="wrapper">
      <div class="row">
        {% for block in row %}
          {# ... block code here ... #}
        {% endfor %}
      </div> <!-- .row -->
    </div> <!-- .wrapper -->
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_1**: Hey Sean, did this answer help?  
  
  ---
  
  