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

CC BY-SA 3.0
2017-02-13T17:22:20.230Z
Doesn't like {% switch firstTextBlock.type %} error: Impossible to access an attribute ("type") on a null variable
sarah3585
CC BY-SA 3.0
2017-02-13T19:07:29.387Z
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 %}`
Tom
CC BY-SA 3.0
2017-02-13T17:12:41.983Z
2017-02-13T17:12:41.983Z
Tom
<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>

<matrix>
How would I pull in the first Matrix block including certain blocks only
# 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.
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 %}
        {{ textBlock.textContent|hacksaw(chars='100', append='...') }}

        {% else %}

        {% if textBlock2 %}

        {{ textBlock2.text|hacksaw(chars='100', append='...') }}
        {% 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' %}
        {{ firstTextBlock.textContent|hacksaw(chars='100', append='...') }}

    {% case 'textImage' %}
        {{ firstTextBlock.text|hacksaw(chars='100', append='...') }}

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

---

