Answers.Body: <p>Give this ago.</p>
  
  <p>It will grab the first block that is either "Text" or "Text Image" and then you can use a switch statement to output your template depending on the block type.</p>
  
  <pre><code>{% set firstTextBlock = entry.body.type(['text', 'textImage']).first() %}
  
  {% switch firstTextBlock.type %}
  
      {% case 'text' %}
          &lt;p&gt;{{ firstTextBlock.textContent|hacksaw(chars='100', append='...') }}&lt;/p&gt;
  
      {% case 'textImage' %}
          &lt;p&gt;{{ firstTextBlock.text|hacksaw(chars='100', append='...') }}&lt;/p&gt;
  
  {% endswitch %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-13T17:22:20.230Z
Answers.Text: Doesn't like {% switch firstTextBlock.type %} error: Impossible to access an attribute ("type") on a null variable
Answers.UserDisplayName: sarah3585
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-13T19:07:29.387Z
Answers.Text: It sounds like you might not have any block matching the "text" or "textImage" type in your matrix. That said, I should have wrapped the switch in an if statement to allow for that.
  
  `{% if firstTextBlock %} _switch_ {% endif %}`
Answers.UserDisplayName: Tom
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-13T17:12:41.983Z
Answers.LastActivityDate: 2017-02-13T17:12:41.983Z
Answers.UserDisplayName: Tom
Body: <p>I have a Matrix field setup. I want to be able to pull in the first Matrix block used to print a snippet, but only out of the blocks 'text' or 'textImage'. So the first one used, out of those two only.<a href="https://i.sstatic.net/3rc47.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3rc47.png" alt="enter image description here"></a></p>
  
  <p>This is what I have but it's pulling in the textContent block regardless, even if the text block is first.</p>
  
  <pre><code>    {% set textBlock = entry.body.type('text').first() %}
      {% set textBlock2 = entry.body.type('textImage').first() %}
  
  
      {% if textBlock %}
          &lt;p&gt;{{ textBlock.textContent|hacksaw(chars='100', append='...') }}&lt;/p&gt;
  
          {% else %}
  
          {% if textBlock2 %}
  
          &lt;p&gt;{{ textBlock2.text|hacksaw(chars='100', append='...') }}&lt;/p&gt;
          {% endif %}
  
      {% endif %}
  </code></pre>
  
Tags: <matrix>
Title: How would I pull in the first Matrix block including certain blocks only
ThreadText: # How would I pull in the first Matrix block including certain blocks only
  
  (Asked by: username_0 on 2017-02-13)
  
  I have a Matrix field setup. I want to be able to pull in the first Matrix block used to print a snippet, but only out of the blocks 'text' or 'textImage'. So the first one used, out of those two only.[![enter image description here](https://i.sstatic.net/3rc47.png)](https://i.sstatic.net/3rc47.png)
  
  This is what I have but it's pulling in the textContent block regardless, even if the text block is first.
  
  ```
      {% set textBlock = entry.body.type('text').first() %}
      {% set textBlock2 = entry.body.type('textImage').first() %}
  
      {% if textBlock %}
          <p>{{ textBlock.textContent|hacksaw(chars='100', append='...') }}</p>
  
          {% else %}
  
          {% if textBlock2 %}
  
          <p>{{ textBlock2.text|hacksaw(chars='100', append='...') }}</p>
          {% endif %}
  
      {% endif %}
  
  ```
  
  **Tags:** matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-02-13. Score: 3 | Accepted answer
  
  Give this ago.
  
  It will grab the first block that is either "Text" or "Text Image" and then you can use a switch statement to output your template depending on the block type.
  
  ```
  {% set firstTextBlock = entry.body.type(['text', 'textImage']).first() %}
  
  {% switch firstTextBlock.type %}
  
      {% case 'text' %}
          <p>{{ firstTextBlock.textContent|hacksaw(chars='100', append='...') }}</p>
  
      {% case 'textImage' %}
          <p>{{ firstTextBlock.text|hacksaw(chars='100', append='...') }}</p>
  
  {% endswitch %}
  
  ```
  
  > **Comments:**
  > **username_0**: Doesn't like {% switch firstTextBlock.type %} error: Impossible to access an attribute ("type") on a null variable  
  > **username_1**: It sounds like you might not have any block matching the "text" or "textImage" type in your matrix. That said, I should have wrapped the switch in an if statement to allow for that.  `{% if firstTextBlock %} _switch_ {% endif %}`  
  
  ---
  
  