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