Loop for Similar Fields I'm in a situation where I have a handful of matrix blocks which have similar subfields. For example, I have a "Bios" block where there's an intro etc., but the a few repeating fields, imagine:


Bio 1 Name (bioName1)
Bio 1 Image (bioImage1)
Bio 2 Name (bioName2)
Bio 2 Image (bioImage2)
etc. up to 6


In my template, I have some pretty redundent code when outputting these fields, like:

    {% if block.bioName1 %}
    &lt;div class="bit"&gt;
        &lt;div class="bio"&gt;
            {% for asset in block.bioImage1 %}
            &lt;img src="{{ asset.url }}" alt="" /&gt;
            {% endfor %}
            &lt;header&gt;
                &lt;h3&gt;{{ block.bioName1 }}&lt;/h3&gt;
                &lt;p&gt;{{ block.bioTitle1 }}&lt;/p&gt;
            &lt;/header&gt;
            {{ block.bioText1 }}
        &lt;/div&gt;
    &lt;/div&gt;
    {% endif %}

    {% if block.bioName2 %}
    &lt;div class="bit"&gt;
        &lt;div class="bio"&gt;
            {% for asset in block.bioImage2 %}
            &lt;img src="{{ asset.url }}" alt="" /&gt;
            {% endfor %}
            &lt;header&gt;
                &lt;h3&gt;{{ block.bioName2 }}&lt;/h3&gt;
                &lt;p&gt;{{ block.bioTitle2 }}&lt;/p&gt;
            &lt;/header&gt;
            {{ block.bioText2 }}
        &lt;/div&gt;
    &lt;/div&gt;
    {% endif %}


I'm wondering if it would be possible to somehow set this up as a loop with a counter to increment the vars (the field names always end with the count)? Any ideas? I tried a few things but have had no luck :/
I think this is an excellent use case for twig's attribute function.


  The attribute function can be used to access a "dynamic" attribute of a variable:


{% for i in 1..6 %}
  {% if attribute(block, 'bioName'~i) %}
    &lt;div class="bit"&gt;
      &lt;div class="bio"&gt;
        {% for asset in attribute(block, 'bioImage'~i) %}
          &lt;img src="{{asset.url}}"/&gt;
          ...etc...
        {% endfor %}
      &lt;/div&gt;
    &lt;/div&gt;
  {% endif %}
{% endfor %}