1

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.

<div id="accordion" role="tablist" class="accordion-vt">
{% for block in entry.contentMatrix %}
{% if entry.contentMatrix.first %}

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

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

  {% else %}

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

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

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

  • I'm getting the following error "Unexpected token "operator" of value "=" ("end of statement block" expected)." – JMKelley Jan 20 '18 at 11:52
  • Sry I forgot an = it was a syntax error – Robin Schambach Jan 20 '18 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.