<p>I find accessing content like this pretty unintuitive, but I think you will need to use the Twig <a href="http://twig.sensiolabs.org/doc/functions/attribute.html"><code>attribute</code> method</a>.  Hopefully the below can help keep you moving in the right direction.  I've simplified to just accessing the Matrix Field and I've made an assumption that you know the Matrix field handles you will want to access.</p>

<pre><code>{% for fieldLayoutField in entry.getFieldLayout().getFields() %}
  {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

  {% if field.type == 'Matrix' %}

    {# Dynamically access the Matrix Field on the Entry Model #}
    {% set matrixField = attribute(entry, field.handle) %}
    {% for block in matrixField %}

      {# Output your Matrix Field Handle #}
      {{ field.handle }}

      {# Output your specific fields content #}
      {{ block.fieldHandle }}

    {% endfor %}

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

<p>You may also want to take a look at how Craft handles outputting Forms in the CP in the <code>app/templates/_includes</code> folder, starting with <code>forms.html</code> and reviewing the many related files.   Doing a task like this cleanly really requires a pretty deep level of abstraction if you don't want to make any assumptions about your variable names.</p>

CC BY-SA 3.0
2015-08-19T15:16:00.403Z
2015-08-19T15:16:00.403Z
Ben Parizek
<p>You can use the same syntax on the Matrix block that you're using on the Entry.</p>

<pre><code>{% for fieldLayoutField in entry.getFieldLayout().getFields() %}
  {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

  {% if field.type == 'Matrix' %}

    {% for block in entry[field.handle] %}
      &lt;tr&gt;

        {# output the name of the matrix field #}
        &lt;td&gt;{{field.name}}&lt;/td&gt;

        &lt;td&gt;
          {% for fieldId in block.fieldLayout.fieldIds %}

            {% set field = craft.fields.getFieldById(fieldId) %}

            {# output the name of the nested field and its value #}
            {{field.name}}: {{block[field.handle]}}&lt;br/&gt;

          {% endfor %}
        &lt;/td&gt;
      &lt;/tr&gt;
    {% endfor %}

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

CC BY-SA 3.0
2017-03-23T20:06:14.930Z
2017-03-23T20:06:14.930Z
Rachel Rine
<p>Ok this is similar to a previously asked question <a href="https://craftcms.stackexchange.com/questions/2926/how-to-display-all-fields-belonging-to-current-entry">How to display all fields belonging to current entry?</a></p>

<p>However I'd like to extend this so that it deals with matrix entry types. I have the following but I just need to know how to loop through all the blocks and pull out each blocks entry field handle and content. Any help appreciated. </p>

<pre><code>{# loop through the fields in the entries fieldLayout Model #}
{% for fieldLayoutField in entry.getFieldLayout().getFields() %}

    {# get the field Model from the fieldId #}
    {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

    {% if field.type == 'Matrix' %}
        {% set matrixHandle = field.handle %}
        {% for field in matrixHandle %}
            ...loop through matrix blocks fields and pull out field handles and content...
        {% endfor %}
    {% else %}
        {# print the field handle and the field content  #}
        {{ field.handle }} : {{ entry.getContent()[field.handle] }}
    {% endif %}

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

<field><fieldtypes>
How to loop through matrix block fields and pull out handle and content?
# How to loop through matrix block fields and pull out handle and content?

(Asked by: username_0 on 2015-08-19)

Ok this is similar to a previously asked question How to display all fields belonging to current entry?
However I'd like to extend this so that it deals with matrix entry types. I have the following but I just need to know how to loop through all the blocks and pull out each blocks entry field handle and content. Any help appreciated. 
{# loop through the fields in the entries fieldLayout Model #}
{% for fieldLayoutField in entry.getFieldLayout().getFields() %}

    {# get the field Model from the fieldId #}
    {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

    {% if field.type == 'Matrix' %}
        {% set matrixHandle = field.handle %}
        {% for field in matrixHandle %}
            ...loop through matrix blocks fields and pull out field handles and content...
        {% endfor %}
    {% else %}
        {# print the field handle and the field content  #}
        {{ field.handle }} : {{ entry.getContent()[field.handle] }}
    {% endif %}

{% endfor %}

**Tags:** field, fieldtypes


---

## 2 answers

### Answer by username_1 on 2015-08-19. Score: 5

I find accessing content like this pretty unintuitive, but I think you will need to use the Twig attribute method.  Hopefully the below can help keep you moving in the right direction.  I've simplified to just accessing the Matrix Field and I've made an assumption that you know the Matrix field handles you will want to access.
{% for fieldLayoutField in entry.getFieldLayout().getFields() %}
  {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

  {% if field.type == 'Matrix' %}

    {# Dynamically access the Matrix Field on the Entry Model #}
    {% set matrixField = attribute(entry, field.handle) %}
    {% for block in matrixField %}

      {# Output your Matrix Field Handle #}
      {{ field.handle }}

      {# Output your specific fields content #}
      {{ block.fieldHandle }}

    {% endfor %}

  {% endif %}
{% endfor %}

You may also want to take a look at how Craft handles outputting Forms in the CP in the app/templates/_includes folder, starting with forms.html and reviewing the many related files.   Doing a task like this cleanly really requires a pretty deep level of abstraction if you don't want to make any assumptions about your variable names.


---

### Answer by username_2 on 2017-03-23. Score: 1

You can use the same syntax on the Matrix block that you're using on the Entry.
{% for fieldLayoutField in entry.getFieldLayout().getFields() %}
  {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

  {% if field.type == 'Matrix' %}

    {% for block in entry[field.handle] %}
      

        {# output the name of the matrix field #}
        {{field.name}}

        
          {% for fieldId in block.fieldLayout.fieldIds %}

            {% set field = craft.fields.getFieldById(fieldId) %}

            {# output the name of the nested field and its value #}
            {{field.name}}: {{block[field.handle]}}

          {% endfor %}
        
      
    {% endfor %}

  {% endif %}
{% endfor %}


---

