<p>What about using the <code>length</code> filter on a <a href="http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type" rel="nofollow">type filtered</a> array of <a href="http://buildwithcraft.com/docs/templating/matrixblockmodel" rel="nofollow">block models</a> to get the total number of your "text" blocks. And then combine that with a counter in your block include to conditionally output the buttons? </p>

<p>_entry.html:</p>

<pre><code>{% set lastTextBlock = entry.contentBlocks.type('text')|length %}
{% set loopIndexText = 0 %}

{% for block in entry.contentBlocks %}
    {% include '_blocks/' ~ block.type|lower %}
{% endfor %}
</code></pre>

<p>_blocks/text.html:</p>

<pre><code>{% set loopIndexText = loopIndexText + 1 %}

{% if loopIndexText == lastTextBlock %}
    ...
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-08-31T17:15:57.727Z
2014-09-03T11:41:57.500Z
2014-09-03T11:41:57.500Z
carlcs
<p>I've come up with this very "manual" approach, which works ok: </p>

<pre><code>{% set lastTextBlock = -1 %}
{% for block in entry.contentBlocks %}
    {% if block.type=='text' %}
        {% set lastTextBlock = loop.index0 %}
    {% endif %}
{% endfor %}

{% for block in entry.contentBlocks %}
    {% include '_blocks/' ~ (block.type | lower) with { 'addButtons': loop.index0==lastTextBlock } %}
{% endfor %}
</code></pre>

<p>But is there a better way? It feels a bit dirty and inefficient to loop over the blocks two times.</p>

CC BY-SA 3.0
2014-08-31T13:34:36.980Z
2014-08-31T13:34:36.980Z
André Elvan
<p>What about comparing the currently looped <a href="http://buildwithcraft.com/docs/templating/matrixblockmodel#id" rel="nofollow">block ID</a> with the ID of the last <a href="http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type" rel="nofollow">block of type</a> "text"?</p>

<p>_entry.html:</p>

<pre><code>{% set lastTextBlock = entry.simpleMatrix.type('text').last() %}
{% set lastTextBlockId = lastTextBlock ? lastTextBlock.id : null %}

{% for block in entry.contentBlocks %}
    {% include '_blocks/' ~ block.type|lower %}
{% endfor %}
</code></pre>

<p>_blocks/text.html:</p>

<pre><code>{% if block.id == lastTextBlockId %}
    ...
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-09-03T12:24:29.547Z
That is quite clever! It's the cleanest solution by far. For some reason it adds an extra query, but the impact on performance is probably negligible. I'm opting for clean code. :)
André Elvan
CC BY-SA 3.0
2014-09-03T12:33:31.137Z
@aelvan for better performance you could save that last "text" block's ID in a var before looping though your blocks. I'll update the answer, even if it makes the code look a little less clean :)
carlcs
CC BY-SA 3.0
2014-09-03T12:35:12.293Z
I still have one issue with the approach (maybe this also applies to the other approaches?). It doesn't update with Live Preview from the CP, this probably has s.th. to do with caching?
carlcs
CC BY-SA 3.0
2014-09-03T11:39:29.250Z
2016-07-27T16:16:09.217Z
2016-07-27T16:16:09.217Z
carlcs
<p>I have a Matrix field with some different block types; images, videos, text, etc. When I loop over the blocks I have some extra code (facebook and twitter sharing, back-button, etc) that should only be output for the last "text" block. </p>

<p>How can I know, inside the loop, if the current block is the last of its type?</p>

CC BY-SA 3.0
2014-09-03T10:22:29.980Z
Did you try my approach, aelvan? I had issues with the type filter, but 2.2 fixed this for me.
carlcs
CC BY-SA 3.0
2014-09-03T11:05:18.737Z
Yes, I tried it, and it works. But I don't think it's a much better solution than mine. :) The increment of the loopIndexText variable inside the include actually makes it a bit more obscure to me. Also, if you needed to use the with { ... } only functionality of the include method, I don't think this would work? 

What I was hoping was that there was some kind of block.lastOfType property that wasn't documented. :)
André Elvan
CC BY-SA 3.0
2014-09-03T11:05:56.707Z
And regarding performance, both methods add one query extra.
André Elvan
CC BY-SA 3.0
2014-09-03T11:45:37.283Z
Yes you're right, it's not that much better. But now that the type filter works for me I tried a new idea. Your mentioning of `block.lastOfType` guided me in the right direction. ;) See new answer below!
carlcs
<matrix><block-types><blocks>
How can I get the last Matrix block of a certain type?
# How can I get the last Matrix block of a certain type?

(Asked by: username_0 on 2014-08-31)

I have a Matrix field with some different block types; images, videos, text, etc. When I loop over the blocks I have some extra code (facebook and twitter sharing, back-button, etc) that should only be output for the last "text" block.

How can I know, inside the loop, if the current block is the last of its type?

**Tags:** matrix, block-types, blocks

> **Comments:**
> **username_1**: Did you try my approach, aelvan? I had issues with the type filter, but 2.2 fixed this for me.  
> **username_0**: Yes, I tried it, and it works. But I don't think it's a much better solution than mine. :) The increment of the loopIndexText variable inside the include actually makes it a bit more obscure to me. Also, if you needed to use the with { ... } only functionality of the include method, I don't think this would work?   What I was hoping was that there was some kind of block.lastOfType property that wasn't documented. :)  
> **username_0**: And regarding performance, both methods add one query extra.  
> **username_1**: Yes you're right, it's not that much better. But now that the type filter works for me I tried a new idea. Your mentioning of `block.lastOfType` guided me in the right direction. ;) See new answer below!  

---

## 3 answers

### Answer by username_1 on 2014-09-03. Score: 5 | Accepted answer

What about comparing the currently looped block ID with the ID of the last block of type "text"?
_entry.html:
{% set lastTextBlock = entry.simpleMatrix.type('text').last() %}
{% set lastTextBlockId = lastTextBlock ? lastTextBlock.id : null %}

{% for block in entry.contentBlocks %}
    {% include '_blocks/' ~ block.type|lower %}
{% endfor %}

_blocks/text.html:
{% if block.id == lastTextBlockId %}
    ...
{% endif %}

> **Comments:**
> **username_0**: That is quite clever! It's the cleanest solution by far. For some reason it adds an extra query, but the impact on performance is probably negligible. I'm opting for clean code. :)  
> **username_1**: @aelvan for better performance you could save that last "text" block's ID in a var before looping though your blocks. I'll update the answer, even if it makes the code look a little less clean :)  
> **username_1**: I still have one issue with the approach (maybe this also applies to the other approaches?). It doesn't update with Live Preview from the CP, this probably has s.th. to do with caching?  

---

### Answer by username_1 on 2014-08-31. Score: 2

What about using the length filter on a type filtered array of block models to get the total number of your "text" blocks. And then combine that with a counter in your block include to conditionally output the buttons? 
_entry.html:
{% set lastTextBlock = entry.contentBlocks.type('text')|length %}
{% set loopIndexText = 0 %}

{% for block in entry.contentBlocks %}
    {% include '_blocks/' ~ block.type|lower %}
{% endfor %}

_blocks/text.html:
{% set loopIndexText = loopIndexText + 1 %}

{% if loopIndexText == lastTextBlock %}
    ...
{% endif %}


---

### Answer by username_0 on 2014-08-31. Score: 1

I've come up with this very "manual" approach, which works ok: 
{% set lastTextBlock = -1 %}
{% for block in entry.contentBlocks %}
    {% if block.type=='text' %}
        {% set lastTextBlock = loop.index0 %}
    {% endif %}
{% endfor %}

{% for block in entry.contentBlocks %}
    {% include '_blocks/' ~ (block.type | lower) with { 'addButtons': loop.index0==lastTextBlock } %}
{% endfor %}

But is there a better way? It feels a bit dirty and inefficient to loop over the blocks two times.


---

