<p>You could first set a variable to test against and then change it the first time the block type appears.</p>

<pre><code>{% set firstTime = true %}

{% for block in entry.matrixField %}

    {% if block.type == "text" and firstTime == true %}

        ...Do something once...

        {% set firstTime = false %}

    {% endif %}

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

CC BY-SA 3.0
2015-04-19T03:16:09.050Z
2015-04-19T03:16:09.050Z
Dylan T
<p>I want to include a conditional for when the 'Text' block type is returned for the first time in a loop. In css it would be first-of-type.</p>

<p>Here's what I have at the moment:</p>

<pre><code>{% if type == 'text' and loop.first %}
    &lt;div class="centered spacer-icon m-all t-all d-all"&gt;&lt;img src="resources/img/minus-icon.svg" alt="Minus Icon" width="30" /&gt;&lt;/div&gt;
{% endif %}
</code></pre>

<p>In this instance my text block type is loop index 3 but I want to adjust the conditional because it is the first text type in the matrix field.</p>

<matrix><conditional><loop>
How Can I Include a Conditional for First Type of Matrix
# How Can I Include a Conditional for First Type of Matrix

(Asked by: username_0 on 2015-04-19)

I want to include a conditional for when the 'Text' block type is returned for the first time in a loop. In css it would be first-of-type.
Here's what I have at the moment:
{% if type == 'text' and loop.first %}
    
{% endif %}

In this instance my text block type is loop index 3 but I want to adjust the conditional because it is the first text type in the matrix field.

**Tags:** matrix, conditional, loop


---

## 1 answers

### Answer by username_1 on 2015-04-19. Score: 15 | Accepted answer

You could first set a variable to test against and then change it the first time the block type appears.
{% set firstTime = true %}

{% for block in entry.matrixField %}

    {% if block.type == "text" and firstTime == true %}

        ...Do something once...

        {% set firstTime = false %}

    {% endif %}

{% endfor %}


---

