How do I get the column headers of a table in a matrix? 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:

&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;
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' %}

                &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 %}