1

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:

<table>
  <thead>
    {{ loop through table headers }}
  </thead>
  <tbody>
    {{ loop through table data }}
  </tbody>
</table>
  • Do you mean field names within the matrix? or the column names of a table field within the matrix? See if this post 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 Feb 3 '15 at 19:10
  • I waned to get the column headers of these types of fields: buildwithcraft.com/docs/table-fields I will check out your link though. .getField() looks to be about what I need. – Bill Criswell Feb 3 '15 at 19:10
  • I don't seem to be able to get to the column headers through your approach, Douglas. – Bill Criswell Feb 3 '15 at 19:26
  • one sec. working on it. – Douglas McDonald Feb 3 '15 at 19:27
  • Awesome. Thanks Douglas. I think I'm getting some terminology mixed up and losing my path along the documentation way. – Bill Criswell Feb 3 '15 at 19:41
2

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

                <h3>{{ field.getField().name }}</h3>

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

        {% endswitch %}

    {% endfor %}

{% endfor %}
|improve this answer|||||
  • Awesome. I will look into inspector. I expected that to be a bit more straight forward than that. – Bill Criswell Feb 3 '15 at 20:32
  • 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 Feb 3 '15 at 21:02
  • 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 Feb 3 '15 at 21:15

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.