<p>Not sure what you want to do with the fields, but you can access them through fieldLayout.</p>

<pre><code>{% for block in entry.myMatrixField %}

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

        {# get properties #}
        {{ field.getField().name }}
        {{ field.getField().handle }}
        {{ field.getField().id }}

        {# get value #}
        {% set handle = field.getField().handle %}
        {{ block[handle] }}

    {% endfor %}

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

<p>You do kind of need to know what the field types are, in order to know how to display them. <code>{{ block[handle] }}</code> will fail for instance if one of the fields is an 'Entry' field, because you can't convert an ElementCriteriaModel to a string. To check the field type you can use <code>field.getField().type</code>;</p>

CC BY-SA 3.0
2015-02-03T20:30:45.923Z
It's a bit more verbose than I was hoping for, but does what I need. Thank you.
ray
CC BY-SA 3.0
2015-02-03T21:18:01.777Z
Get rid of the extra junk and it's just 5 lines! Fyi... see [this answer](http://craftcms.stackexchange.com/a/6964/784) for more complete example.
Douglas McDonald
CC BY-SA 3.0
2015-02-03T22:22:32.723Z
I just meant that `block[block.getFieldLayout().getFields()[0].getField().handle]` seems like a long dig to get to the value.
ray
CC BY-SA 3.0
2015-02-02T23:34:30.903Z
2015-02-02T23:41:41.343Z
2015-02-02T23:41:41.343Z
Douglas McDonald
<p>Is there a way to dynamically iterate over a matrix block's fields such that the template doesn't need to know the field handles? This doesn't work:</p>

<pre><code>{% for block in entry.myMatrixField %}
    {% for field in block %}
        {# do something with field #}       
    {% endfor %}
{% endfor %}
</code></pre>

<p>This seems like a simple thing to want to do but I haven't figured out a way to do it.</p>

<matrix><field>
Dynamically iterate over a matrix block's fields?
# Dynamically iterate over a matrix block's fields?

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

Is there a way to dynamically iterate over a matrix block's fields such that the template doesn't need to know the field handles? This doesn't work:
{% for block in entry.myMatrixField %}
    {% for field in block %}
        {# do something with field #}       
    {% endfor %}
{% endfor %}

This seems like a simple thing to want to do but I haven't figured out a way to do it.

**Tags:** matrix, field


---

## 1 answers

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

Not sure what you want to do with the fields, but you can access them through fieldLayout.
{% for block in entry.myMatrixField %}

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

        {# get properties #}
        {{ field.getField().name }}
        {{ field.getField().handle }}
        {{ field.getField().id }}

        {# get value #}
        {% set handle = field.getField().handle %}
        {{ block[handle] }}

    {% endfor %}

{% endfor %}

You do kind of need to know what the field types are, in order to know how to display them. {{ block[handle] }} will fail for instance if one of the fields is an 'Entry' field, because you can't convert an ElementCriteriaModel to a string. To check the field type you can use field.getField().type;

> **Comments:**
> **username_0**: It's a bit more verbose than I was hoping for, but does what I need. Thank you.  
> **username_1**: Get rid of the extra junk and it's just 5 lines! Fyi... see [this answer](http://craftcms.stackexchange.com/a/6964/784) for more complete example.  
> **username_0**: I just meant that `block[block.getFieldLayout().getFields()[0].getField().handle]` seems like a long dig to get to the value.  

---

