Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-31T17:15:57.727Z
Answers.LastActivityDate: 2014-09-03T11:41:57.500Z
Answers.LastEditDate: 2014-09-03T11:41:57.500Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-31T13:34:36.980Z
Answers.LastActivityDate: 2014-08-31T13:34:36.980Z
Answers.UserDisplayName: André Elvan
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-03T12:24:29.547Z
Answers.Text: 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. :)
Answers.UserDisplayName: André Elvan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-03T12:33:31.137Z
Answers.Text: @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 :)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-03T12:35:12.293Z
Answers.Text: 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?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-03T11:39:29.250Z
Answers.LastActivityDate: 2016-07-27T16:16:09.217Z
Answers.LastEditDate: 2016-07-27T16:16:09.217Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-03T10:22:29.980Z
Comments.Text: Did you try my approach, aelvan? I had issues with the type filter, but 2.2 fixed this for me.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-03T11:05:18.737Z
Comments.Text: 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. :)
Comments.UserDisplayName: André Elvan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-03T11:05:56.707Z
Comments.Text: And regarding performance, both methods add one query extra.
Comments.UserDisplayName: André Elvan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-03T11:45:37.283Z
Comments.Text: 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!
Comments.UserDisplayName: carlcs
Tags: <matrix><block-types><blocks>
Title: How can I get the last Matrix block of a certain type?
ThreadText: # 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](http://buildwithcraft.com/docs/templating/matrixblockmodel#id) with the ID of the last [block of type](http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-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](http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type) array of [block models](http://buildwithcraft.com/docs/templating/matrixblockmodel) 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.
  
  
  ---
  
  