<p>Your condition <code>{% if entry.contentMatrix.first %}</code> will check if there exists a block in your field or not, it does not check if your current block is the first or not. Since there is one the condition will always become true. What you want to do is to check if you are in the first loop or not so I would suggest you to do something like this</p>

<pre><code>{% for key, block in entry.contentMatrix %}
    {% if key == 0 %}
</code></pre>

<p>Or something like that </p>

CC BY-SA 3.0
2018-01-20T11:52:30.987Z
I'm getting the following error "Unexpected token "operator" of value "=" ("end of statement block" expected)."
JMKelley
CC BY-SA 3.0
2018-01-20T12:06:11.393Z
Sry I forgot an `=` it was a syntax error
Robin Schambach
CC BY-SA 3.0
2018-01-19T21:30:22.687Z
2018-01-20T12:05:27.970Z
2018-01-20T12:05:27.970Z
Robin Schambach
<p>I'm trying to target the first matrix block and changing the structure of the HTML, but nesting an IF statement within the loop but it's actually not doing anything, instead it's just displaying both the same. </p>

<pre><code>&lt;div id="accordion" role="tablist" class="accordion-vt"&gt;
{% for block in entry.contentMatrix %}
{% if entry.contentMatrix.first %}

  &lt;div class="card"&gt;
    &lt;div class="card-header" role="tab" id="headingOne"&gt;
      &lt;h1 class="mb-0"&gt;
        &lt;a data-toggle="collapse" href="#collapseOne" role="button" aria-expanded="true" aria-controls="collapseOne"&gt;
          {{ block.contentTitle }}
        &lt;/a&gt;
      &lt;/h1&gt;
    &lt;/div&gt;

    &lt;div id="collapseOne" class="collapse show" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion"&gt;
      &lt;div class="card-body"&gt;
        {{ block.contentDescription }}
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;

  {% else %}

  &lt;div class="card"&gt;
    &lt;div class="card-header" role="tab" id="headingOne"&gt;
      &lt;h1 class="mb-0"&gt;
        &lt;a data-toggle="collapse" href="#collapseOne" role="button" aria-expanded="true" aria-controls="collapseOne"&gt;
          {{ block.contentTitle }}
        &lt;/a&gt;
      &lt;/h1&gt;
    &lt;/div&gt;

    &lt;div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion"&gt;
      &lt;div class="card-body"&gt;
        {{ block.contentDescription }}
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
{% endfor %}
&lt;/div&gt;
</code></pre>

<templating><entry><forloop>
Changing first matrix block
# Changing first matrix block

(Asked by: username_0 on 2018-01-19)

I'm trying to target the first matrix block and changing the structure of the HTML, but nesting an IF statement within the loop but it's actually not doing anything, instead it's just displaying both the same. 

{% for block in entry.contentMatrix %}
{% if entry.contentMatrix.first %}

  
    
      
        
          {{ block.contentTitle }}
        
      
    

    
      
        {{ block.contentDescription }}
      
    
  

  {% else %}

  
    
      
        
          {{ block.contentTitle }}
        
      
    

    
      
        {{ block.contentDescription }}
      
    
  
{% endfor %}

**Tags:** templating, entry, forloop


---

## 1 answers

### Answer by username_1 on 2018-01-19. Score: 2 | Accepted answer

Your condition {% if entry.contentMatrix.first %} will check if there exists a block in your field or not, it does not check if your current block is the first or not. Since there is one the condition will always become true. What you want to do is to check if you are in the first loop or not so I would suggest you to do something like this
{% for key, block in entry.contentMatrix %}
    {% if key == 0 %}

Or something like that

> **Comments:**
> **username_0**: I'm getting the following error "Unexpected token "operator" of value "=" ("end of statement block" expected)."  
> **username_1**: Sry I forgot an `=` it was a syntax error  

---

