<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>

CC BY-SA 3.0
2018-04-01T06:00:08.687Z
Hey Sean, did this answer help?
Alex Roper
CC BY-SA 3.0
2018-03-21T19:22:15.940Z
2018-03-21T19:22:15.940Z
Alex Roper
<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>

<templating><forloop><loop><batch>
Use Twig's batch filter with conditional
# 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' %}
  
    
      {% for block in row %}
        
          
            
              {{ block.titleText }}
              {% if block.ctaUrl|length and block.ctaText|length %}
                
                  {{ block.ctaText }}
                
              {% endif %}
             
           
         
      {% endfor %}
     
   
{% 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) %}
  
    
      {% for block in row %}
        {# ... block code here ... #}
      {% endfor %}
     
   
{% endfor %}

> **Comments:**
> **username_1**: Hey Sean, did this answer help?  

---

