<p>Updated code to output the entire table (headers and data).</p>

<p>Try this: </p>

<pre><code>{% set entry = craft.entries.section('sectionHandle').first %}

{% for block in entry.matrixFieldHandle %}

    {% for field in block.getFieldLayout().getFields() %}

        {% set type = field.getField().type %}

        {% switch type %}
            {% case 'Table' %}

                &lt;h3&gt;{{ field.getField().name }}&lt;/h3&gt;

                &lt;table&gt;
                    &lt;thead&gt;
                        &lt;tr&gt;
                            {% set columns = field.getField().settings.columns %}
                            {% for col in columns %}        
                                &lt;th&gt;{{ col.heading }}&lt;/th&gt;
                            {% endfor %}
                        &lt;/tr&gt;
                    &lt;/thead&gt;
                    &lt;tbody&gt;
                            {% set handle = field.getField().handle %}
                            {% set tableData = block[handle] %}
                            {% for row in tableData %}
                                &lt;tr&gt;
                                    {% for key, col in row if key|slice(0,3) == 'col' %}
                                        &lt;td&gt;{{ col }}&lt;/td&gt;
                                    {% endfor %}
                                &lt;/tr&gt;
                            {% endfor %}
                    &lt;/tbody&gt;
                &lt;/table&gt;

        {% endswitch %}

    {% endfor %}

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

CC BY-SA 3.0
2015-02-03T20:32:06.783Z
Awesome. I will look into inspector. I expected that to be a bit more straight forward than that.
Bill Criswell
CC BY-SA 3.0
2015-02-03T21:02:06.020Z
I updated the answer to output the entire table — headers and data. Now just need to do this for each field type and we've got an auto-matrix builder ;)
Douglas McDonald
CC BY-SA 3.0
2015-02-03T21:15:12.340Z
If you should happen to write that up let me know! I'll be giving it a shot but you seem more familiar with Craft than I am!
Bill Criswell
CC BY-SA 3.0
2015-02-03T19:58:32.143Z
2015-02-03T20:59:21.510Z
2015-02-03T20:59:21.510Z
Douglas McDonald
<p>I'm trying to write a general purpose matrix template and I've come up on a little snag with tables fields. I see no way to get the column header values. </p>

<p>The answer <a href="https://craftcms.stackexchange.com/questions/2148/show-a-table-fields-column-headers-in-template">here</a> mentions <code>craft.fields.getFieldById(block.fieldId)</code> but that doesn't appear to exist according to the documentation, nor did it work properly. </p>

<p>The closest I've gotten is this dirty slice job and that only gives me the ids of the columns. So I can do some string manipulation to get them close to the value but they can be completely different as well.</p>

<pre><code>{% set first = block.table | first %}
{% set columns = first | keys | slice((first | length) / 2, first | length) %}
</code></pre>

<p>Any ideas would be helpful!</p>

<p>I'm trying to accomplish this:</p>

<pre><code>&lt;table&gt;
  &lt;thead&gt;
    {{ loop through table headers }}
  &lt;/thead&gt;
  &lt;tbody&gt;
    {{ loop through table data }}
  &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

CC BY-SA 3.0
2015-02-03T19:10:41.880Z
Do you mean field names within the matrix? or the column names of a table field within the matrix? See if [this post](http://craftcms.stackexchange.com/a/6936/784) answers your question. As a side note I highly recommend a plugin called 'inspector' for printing out methods and properties of objects (i.e. {{ block|inspect }} will show you all properties).
Douglas McDonald
CC BY-SA 3.0
2015-02-03T19:10:47.083Z
I waned to get the column headers of these types of fields: http://buildwithcraft.com/docs/table-fields I will check out your link though. .getField() looks to be about what I need.
Bill Criswell
CC BY-SA 3.0
2015-02-03T19:26:29.773Z
I don't seem to be able to get to the column headers through your approach, Douglas.
Bill Criswell
CC BY-SA 3.0
2015-02-03T19:27:56.227Z
one sec. working on it.
Douglas McDonald
CC BY-SA 3.0
2015-02-03T19:41:11.253Z
Awesome. Thanks Douglas. I think I'm getting some terminology mixed up and losing my path along the documentation way.
Bill Criswell
<matrix><templating><table>
How do I get the column headers of a table in a matrix?
# How do I get the column headers of a table in a matrix?

(Asked by: username_0 on 2015-02-03)

I'm trying to write a general purpose matrix template and I've come up on a little snag with tables fields. I see no way to get the column header values. 
The answer here mentions craft.fields.getFieldById(block.fieldId) but that doesn't appear to exist according to the documentation, nor did it work properly. 
The closest I've gotten is this dirty slice job and that only gives me the ids of the columns. So I can do some string manipulation to get them close to the value but they can be completely different as well.
{% set first = block.table | first %}
{% set columns = first | keys | slice((first | length) / 2, first | length) %}

Any ideas would be helpful!
I'm trying to accomplish this:

  
    {{ loop through table headers }}
  
  
    {{ loop through table data }}

**Tags:** matrix, templating, table

> **Comments:**
> **username_1**: Do you mean field names within the matrix? or the column names of a table field within the matrix? See if [this post](http://craftcms.stackexchange.com/a/6936/784) answers your question. As a side note I highly recommend a plugin called 'inspector' for printing out methods and properties of objects (i.e. {{ block|inspect }} will show you all properties).  
> **username_0**: I waned to get the column headers of these types of fields: http://buildwithcraft.com/docs/table-fields I will check out your link though. .getField() looks to be about what I need.  
> **username_0**: I don't seem to be able to get to the column headers through your approach, Douglas.  
> **username_1**: one sec. working on it.  
> **username_0**: Awesome. Thanks Douglas. I think I'm getting some terminology mixed up and losing my path along the documentation way.  

---

## 1 answers

### Answer by username_1 on 2015-02-03. Score: 2 | Accepted answer

Updated code to output the entire table (headers and data).
Try this: 
{% set entry = craft.entries.section('sectionHandle').first %}

{% for block in entry.matrixFieldHandle %}

    {% for field in block.getFieldLayout().getFields() %}

        {% set type = field.getField().type %}

        {% switch type %}
            {% case 'Table' %}

                {{ field.getField().name }}

                
                    
                        
                            {% set columns = field.getField().settings.columns %}
                            {% for col in columns %}        
                                {{ col.heading }}
                            {% endfor %}
                        
                    
                    
                            {% set handle = field.getField().handle %}
                            {% set tableData = block[handle] %}
                            {% for row in tableData %}
                                
                                    {% for key, col in row if key|slice(0,3) == 'col' %}
                                        {{ col }}
                                    {% endfor %}
                                
                            {% endfor %}
                    
                

        {% endswitch %}

    {% endfor %}

{% endfor %}

> **Comments:**
> **username_0**: Awesome. I will look into inspector. I expected that to be a bit more straight forward than that.  
> **username_1**: I updated the answer to output the entire table — headers and data. Now just need to do this for each field type and we've got an auto-matrix builder ;)  
> **username_0**: If you should happen to write that up let me know! I'll be giving it a shot but you seem more familiar with Craft than I am!  

---

