I want to see if a certain field exists in a block of a matrix. Here is my code.

{% for block in entry.someStepContainer %}
  <div class="each-step">

    {% if block.type == "someStep" %}
      <div class="step-number"><span>{{loop.index}}</span></div>
      <h3 class="step-title">{{block.stepTitle}}</h3>
      <div class="step-description">{{block.stepDescription}}</div>

      <! -- this is part I'm trying to fix -->

      {% if block.stepAccordionContent %}
        <div class="accordion-section">
          {% for row in block.stepAccordionContent %}
            <h3 class="accordion-control">{{row.accordionTitle}}</h3>
            <p class="accordion-content" style="display: none;">{{row.accordionContent}}</p>
          {% endfor %}
      {% endif %}

      <!-- end part -->

    {% endif %}

{% endfor %} 

I've tried using:

{% if block.stepAccordionContent | length %}

But this still yields an empty

<div class="accordion-section">

stepAccordionContent is a table.

  • You say your checking if a Matrix block exists, but it looks like you're checking if a field inside of a block has any data. Can you clarify what stepAccordionContent is? – Brad Bell Jan 6 '15 at 0:50
  • @DouglasMcDonald printing {{block.stepAccordionContent|length}} gives me 0. – Nick Jan 6 '15 at 16:12
  • @BradBell you're right. I have a matrix that is someStepsContainer with one block someStep and three fields, one of them being stepAccordionContent which is a table. – Nick Jan 6 '15 at 16:14
  • @BradBell stepAccordionContent is a table – Nick Jan 6 '15 at 16:21
  • @DouglasMcDonald I was expecting if {%if block.stepAccordionContent|length%} returns 0, nothing inside the conditional would show. Instead, all content is showing (empty h3 and p) – Nick Jan 6 '15 at 17:16

You can use {% if block.stepAccordionContent|length %}...{% endif %} to test if any rows are defined in your table field.

Note: When you create a table field it will have an empty row defined under 'Default Values', which should be removed; otherwise all new instances of the table will have an empty row defined by default causing block.stepAccordionContent|length to return 1, instead of 0 as expected.

| improve this answer | |
  • I'd also make sure you're looking at the correct fellowshipStep block since there could be multiple ones on the entry. – Brad Bell Jan 6 '15 at 22:19
  • My Test1: showed '1' for each, every 'foo' showed up, and in my entry with stepAccordionContent, Test1 showed '5' and the accordion-selection div, along with foo's – Nick Jan 6 '15 at 23:47
  • I think I may have figured out your problem. In the table field settings in your matrix field, there is something called 'Default Values'. I would guess that you have one empty row defined, such that when you create a new matrix block, by default it has one empty row in it; thus when you run your test you get 1 for length, even though it looks empty. If you actually delete the empty row I would guess that it returns zero as expected. Go into your settings and delete that extra row from the 'Default Values', and the problem should go away. – Douglas McDonald Jan 7 '15 at 0:21
  • @DouglasMcDonald I did have an empty row as a default value! Once I deleted this from the field settings, then went and deleted it for each entry already created, it worked!!!!! Bravo bravo!!!! – Nick Jan 7 '15 at 0:26
  • You should add this as an answer! – Nick Jan 7 '15 at 0:26

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.