Show a table field's column headers in template? The question says it all.

I have:


A Matrix field, with
A subfield with the type table
Two columns within that table of which I want to show the column headers in my template


Is that possible and if yes, how?
You don't get table headings with the entry model data (use the dump() function while in dev mode to see yourself). But there's the method getFieldByHandle() that returns a field model containing info to any field settings.

This is how you access the headings of a table field

Craft 2

{% set field = craft.fields.getFieldByHandle('myTableField') %}

{% for column in field.settings.columns %}
    &lt;li&gt;{{ column.heading }}&lt;/li&gt;
{% endfor %}


Craft 3

{% set field = craft.app.fields.getFieldByHandle('myTableField') %}

{% for column in field.settings.columns %}
    &lt;li&gt;{{ column.heading }}&lt;/li&gt;
{% endfor %}


Table field in Matrix block:

As fields within matrix blocks don't necessarily have unique field handles, you can't use the getFieldByHandle() method to get the field model. Use the unique field ID with the method getFieldById() instead. To look that ID up, open up the source code of the field in the matrix field's field settings. There's a div with the class matrixconfigitem and a data attribute data-id, which is the ID you are looking for.

(PS: use the code in your matrix loop for the block type containing your table "sub" field to output in the right place, but it actually works everywhere!)