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>
5
  • 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). Commented Feb 3, 2015 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. Commented Feb 3, 2015 at 19:10
  • I don't seem to be able to get to the column headers through your approach, Douglas. Commented Feb 3, 2015 at 19:26
  • one sec. working on it. Commented Feb 3, 2015 at 19:27
  • Awesome. Thanks Douglas. I think I'm getting some terminology mixed up and losing my path along the documentation way. Commented Feb 3, 2015 at 19:41

1 Answer 1

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 %}
3
  • Awesome. I will look into inspector. I expected that to be a bit more straight forward than that. Commented Feb 3, 2015 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 ;) Commented Feb 3, 2015 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! Commented Feb 3, 2015 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.