<p>Getting the second instance of a particular block type is fairly easy. </p>

<pre><code>{% set textBlockCount = 0 %}
{% for block in entry.postBody %}

    {% include 'blog/blocks/_' ~ block.type ignore missing with {"block":block} only %}

    {% if block.type == "text" %}
        {% set textBlockCount = textBlockCount + 1 %}
    {% endif %}

    {% if textBlockCount == 2 %}
        {# insert optional content here #}
        {% set textBlockCount = 0 %}
    {% endif %}

{% endfor %}
</code></pre>

<p>And you can get the last block (ignoring blockType) using a <code>{% if loop.last %}...{% endif %}</code> conditional. </p>

<p>However, getting the last instance of a blocktype is a bit more difficult, because it would require you to be able to "look ahead" in the loop to determine if it is the last of its type, which isn't really possible. I would suggest perhaps adding the optional data into one of the blocktypes directly or adding a lightswitch or checkbox field to the blocktype to trigger the optional include).</p>

<pre><code>{% if block.type == 'text' and block.includeOptional %}
    {# insert optional content here #}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-05-05T16:39:58.797Z
2015-05-05T16:47:01.253Z
2015-05-05T16:47:01.253Z
Douglas McDonald
<p>You could save that second text block's ID to a variable and compare against that in your text partial:</p>

<p>_entry.html:</p>

<pre><code>{% set textBlocksIds = entry.postBody.type('text').ids() %}
{% set secondTextBlockId = textBlocksIds|length &gt;= 2 ? textBlocksIds[2] : textBlocksIds[1] %}

{% for block in entry.postBody %}
    {% include 'blog/blocks/_' ~ block.type ignore missing with {
        'block': block,
        'secondTextBlockId': secondTextBlockId
    } only %}
{% endfor %}
</code></pre>

<p>blog/blocks/_text.html:</p>

<pre><code>{% if block.id == secondTextBlockId %}
    {% include 'blog/partials/_restaurants' %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-05-05T16:44:48.563Z
Much better solution!
Douglas McDonald
CC BY-SA 3.0
2015-05-05T17:13:27.010Z
This works perfectly thank you! Had to edit the array keys to be one less as its zero-based :)
Seán O'Grady
CC BY-SA 3.0
2015-05-05T17:19:40.540Z
Great, @SeanO'Grady! Don't forget to cover those cases when there's no `textBlock` at all (e.g. Live Preview). I just didn't want to make that conditional to complicated.
carlcs
CC BY-SA 3.0
2015-05-05T16:27:14.110Z
2015-05-05T16:27:14.110Z
carlcs
<p>We use a matrix field for the article content on our site. We have different blocks for different content (text, images, embed etc..) and we have optional fields that are outside of the matrix that  only need to be defined once, like a related article or place.</p>

<p>My current code to output the matrix is as follows</p>

<pre><code>{% for block in entry.postBody %}

{% include 'blog/blocks/_' ~ block.type ignore missing with {"block":block} only %}

{% endfor %}
</code></pre>

<p>I want to float my related related restaurant section (which is optional) after either</p>

<ul>
<li>the second instance of a <code>text</code> block</li>
</ul>

<p>or</p>

<ul>
<li>the last <code>text</code> block. </li>
</ul>

<p>Some of my blocks are elements that take up the entire width of the section, so I want to attach it to a text block so that it floats neatly alongside the text.</p>

<p>How would I select the second or last instance of a certain block type within my for loop?</p>

<matrix><forloop>
How Do I Select the second instance of a certain block type
# How Do I Select the second instance of a certain block type

(Asked by: username_0 on 2015-05-05)

We use a matrix field for the article content on our site. We have different blocks for different content (text, images, embed etc..) and we have optional fields that are outside of the matrix that  only need to be defined once, like a related article or place.
My current code to output the matrix is as follows
{% for block in entry.postBody %}

{% include 'blog/blocks/_' ~ block.type ignore missing with {"block":block} only %}

{% endfor %}

I want to float my related related restaurant section (which is optional) after either

the second instance of a text block

or

the last text block. 

Some of my blocks are elements that take up the entire width of the section, so I want to attach it to a text block so that it floats neatly alongside the text.
How would I select the second or last instance of a certain block type within my for loop?

**Tags:** matrix, forloop

---

## 2 answers

### Answer by username_2 on 2015-05-05. Score: 4 | Accepted answer

You could save that second text block's ID to a variable and compare against that in your text partial:
_entry.html:
{% set textBlocksIds = entry.postBody.type('text').ids() %}
{% set secondTextBlockId = textBlocksIds|length >= 2 ? textBlocksIds[2] : textBlocksIds[1] %}

{% for block in entry.postBody %}
    {% include 'blog/blocks/_' ~ block.type ignore missing with {
        'block': block,
        'secondTextBlockId': secondTextBlockId
    } only %}
{% endfor %}

blog/blocks/_text.html:
{% if block.id == secondTextBlockId %}
    {% include 'blog/partials/_restaurants' %}
{% endif %}

> **Comments:**
> **username_1**: Much better solution!  
> **username_0**: This works perfectly thank you! Had to edit the array keys to be one less as its zero-based :)  
> **username_2**: Great, @SeanO'Grady! Don't forget to cover those cases when there's no `textBlock` at all (e.g. Live Preview). I just didn't want to make that conditional to complicated.  

---

### Answer by username_1 on 2015-05-05. Score: 2

Getting the second instance of a particular block type is fairly easy. 
{% set textBlockCount = 0 %}
{% for block in entry.postBody %}

    {% include 'blog/blocks/_' ~ block.type ignore missing with {"block":block} only %}

    {% if block.type == "text" %}
        {% set textBlockCount = textBlockCount + 1 %}
    {% endif %}

    {% if textBlockCount == 2 %}
        {# insert optional content here #}
        {% set textBlockCount = 0 %}
    {% endif %}

{% endfor %}

And you can get the last block (ignoring blockType) using a {% if loop.last %}...{% endif %} conditional. 
However, getting the last instance of a blocktype is a bit more difficult, because it would require you to be able to "look ahead" in the loop to determine if it is the last of its type, which isn't really possible. I would suggest perhaps adding the optional data into one of the blocktypes directly or adding a lightswitch or checkbox field to the blocktype to trigger the optional include).
{% if block.type == 'text' and block.includeOptional %}
    {# insert optional content here #}
{% endif %}


---

