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

<p>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 <code>block.stepAccordionContent|length</code> to return 1, instead of 0 as expected.</p>

CC BY-SA 3.0
2015-01-06T22:19:06.370Z
I'd also make sure you're looking at the correct `fellowshipStep` block since there could be multiple ones on the entry.
Brad Bell
CC BY-SA 3.0
2015-01-06T23:47:56.197Z
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
CC BY-SA 3.0
2015-01-07T00:21:39.817Z
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
CC BY-SA 3.0
2015-01-07T00:26:40.780Z
@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
CC BY-SA 3.0
2015-01-07T00:26:59.253Z
You should add this as an answer!
Nick
CC BY-SA 3.0
2015-01-07T01:28:38.207Z
Great! whew. I updated the answer and deleted all my comments to help clean this up a bit.
Douglas McDonald
CC BY-SA 3.0
2015-01-06T22:13:13.527Z
2015-01-07T01:26:07.343Z
2015-01-07T01:26:07.343Z
Douglas McDonald
<p>I want to see if a certain field exists in a block of a matrix. Here is my code.</p>

<pre><code>{% for block in entry.someStepContainer %}
  &lt;div class="each-step"&gt;

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

      &lt;! -- this is part I'm trying to fix --&gt;

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


      &lt;!-- end part --&gt;

    {% endif %}

  &lt;/div&gt;
{% endfor %} 
</code></pre>

<p>I've tried using:</p>

<pre><code>{% if block.stepAccordionContent | length %}
</code></pre>

<p>But this still yields an empty</p>

<pre><code>&lt;div class="accordion-section"&gt;
</code></pre>

<p><code>stepAccordionContent</code> is a table.</p>

CC BY-SA 3.0
2015-01-06T00:50:22.900Z
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
CC BY-SA 3.0
2015-01-06T16:12:11.060Z
@DouglasMcDonald printing `{{block.stepAccordionContent|length}}` gives me `0`.
Nick
CC BY-SA 3.0
2015-01-06T16:14:57.657Z
@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
CC BY-SA 3.0
2015-01-06T16:21:09.197Z
@BradBell `stepAccordionContent` is a table
Nick
CC BY-SA 3.0
2015-01-06T17:16:02.097Z
@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
CC BY-SA 3.0
2015-01-06T17:24:24.390Z
@DouglasMcDonald doesn't work there either. I'm wondering if it's just checking for the presence of that field, in which case, it's always there, though it may be empty (which it is not checking for)
Nick
CC BY-SA 3.0
2015-01-06T17:26:09.967Z
@DouglasMcDonald are you asking if I've cleared my cache recently? (Sorry, not sure if "using `cache`" is something Craft-specific)
Nick
CC BY-SA 3.0
2015-01-06T17:34:14.477Z
@DouglasMcDonald It did output '0' and nothing else. I do not have it in a `{% cache %}` statement.
Nick
CC BY-SA 3.0
2015-01-06T19:57:58.597Z
FWIW, I just setup a test case locally with a table inside of Matrix and it's working as expected for me.  I'd try to narrow down your template to the most basic for possible and go from there.
Brad Bell
CC BY-SA 3.0
2015-01-06T20:14:52.233Z
@DouglasMcDonald no luck so far, have made a gist https://gist.github.com/nickbytes/37e1bf3a5c03a047d36e and will try and pair things away (the naming is a little different, looking at last section of template)
Nick
CC BY-SA 3.0
2015-01-06T20:15:24.397Z
@BradBell made a gist, will try your advice and make most basic template to see what issue is https://gist.github.com/nickbytes/37e1bf3a5c03a047d36e
Nick
CC BY-SA 3.0
2015-01-06T21:02:06.170Z
@BradBell This seems to be simplest instance https://gist.github.com/nickbytes/8c2a1100b4828d92db98 just checking to see if that field shows each time. It is still showing everything in `<div class="accordion-section">`
Nick
CC BY-SA 3.0
2015-01-06T21:02:21.330Z
@DouglasMcDonald This seems to be simplest instance https://gist.github.com/nickbytes/8c2a1100b4828d92db98 just checking to see if that field shows each time. It is still showing everything in `<div class="accordion-section">`
Nick
<templating><matrix>
How can I tell if a field exists in a block of a matrix?
# How can I tell if a field exists in a block of a matrix?

(Asked by: username_0 on 2015-01-05)

I want to see if a certain field exists in a block of a matrix. Here is my code.
{% for block in entry.someStepContainer %}
  

    {% if block.type == "someStep" %}
      {{loop.index}}
      {{block.stepTitle}}
      {{block.stepDescription}}

      

      {% if block.stepAccordionContent %}
        
          {% for row in block.stepAccordionContent %}
            {{row.accordionTitle}}
            {{row.accordionContent}}
          {% endfor %}
        
      {% endif %}


      

    {% endif %}

  
{% endfor %} 

I've tried using:
{% if block.stepAccordionContent | length %}

But this still yields an empty


stepAccordionContent is a table.

**Tags:** templating, matrix

> **Comments:**
> **username_1**: 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?  
> **username_0**: @DouglasMcDonald printing `{{block.stepAccordionContent|length}}` gives me `0`.  
> **username_0**: @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.  
> **username_0**: @BradBell `stepAccordionContent` is a table  
> **username_0**: @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`)  
> **username_0**: @DouglasMcDonald doesn't work there either. I'm wondering if it's just checking for the presence of that field, in which case, it's always there, though it may be empty (which it is not checking for)  
> **username_0**: @DouglasMcDonald are you asking if I've cleared my cache recently? (Sorry, not sure if "using `cache`" is something Craft-specific)  
> **username_0**: @DouglasMcDonald It did output '0' and nothing else. I do not have it in a `{% cache %}` statement.  
> **username_1**: FWIW, I just setup a test case locally with a table inside of Matrix and it's working as expected for me.  I'd try to narrow down your template to the most basic for possible and go from there.  
> **username_0**: @DouglasMcDonald no luck so far, have made a gist https://gist.github.com/nickbytes/37e1bf3a5c03a047d36e and will try and pair things away (the naming is a little different, looking at last section of template)  
> **username_0**: @BradBell made a gist, will try your advice and make most basic template to see what issue is https://gist.github.com/nickbytes/37e1bf3a5c03a047d36e  
> **username_0**: @BradBell This seems to be simplest instance https://gist.github.com/nickbytes/8c2a1100b4828d92db98 just checking to see if that field shows each time. It is still showing everything in `<div class="accordion-section">`  
> **username_0**: @DouglasMcDonald This seems to be simplest instance https://gist.github.com/nickbytes/8c2a1100b4828d92db98 just checking to see if that field shows each time. It is still showing everything in `<div class="accordion-section">`  

---

## 1 answers

### Answer by username_2 on 2015-01-06. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_1**: I'd also make sure you're looking at the correct `fellowshipStep` block since there could be multiple ones on the entry.  
> **username_0**: 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  
> **username_2**: 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.  
> **username_0**: @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!!!!  
> **username_0**: You should add this as an answer!  
> **username_2**: Great! whew. I updated the answer and deleted all my comments to help clean this up a bit.  

---

