<p>You can use the "subscript" variable syntax to add the loop index to your field names automatically. </p>

<p><a href="http://twig.sensiolabs.org/doc/templates.html#variables" rel="nofollow">http://twig.sensiolabs.org/doc/templates.html#variables</a></p>

<pre><code>{% for i in 1..6 %}
  {% if block['bioName'~i] %}
    &lt;div class="bit"&gt;
      &lt;div class="bio"&gt;
        {% for asset in block['bioImage'~i] %}
          &lt;img src="{{ asset.url }}" alt="" /&gt;
        {% endfor %}
        &lt;header&gt;
          &lt;h3&gt;{{ block['bioName'~i] }}&lt;/h3&gt;
          &lt;p&gt;{{ block['bioTitle'~i] }}&lt;/p&gt;
        &lt;/header&gt;
        {{ block['bioText'~i] }}
      &lt;/div&gt;
    &lt;/div&gt;
  {% endif %}
{% endfor %}
</code></pre>

<p>I prefer this to the attribute function because it's a little simpler to look at. But the attribute function will work in some circumstances where dot notation and subscript syntax can't.</p>

<p>This is taken from the Twig docs link above:</p>

<blockquote>
  <p>When the attribute contains special characters (like - that would be interpreted as the minus operator), use the attribute function instead to access the variable attribute:</p>
</blockquote>

<pre><code>{# equivalent to the non-working foo.data-foo #}
{{ attribute(foo, 'data-foo') }}
</code></pre>

CC BY-SA 3.0
2015-04-13T09:30:16.050Z
2015-04-13T09:30:16.050Z
Alex Roper
<p>I think this is an excellent use case for twig's <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="nofollow">attribute</a> function.</p>

<blockquote>
  <p>The <code>attribute</code> function can be used to access a "dynamic" attribute of a variable:</p>
</blockquote>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2015-04-13T02:18:29.233Z
Way better than my answer! Great idea to use attribute.
Aaron Berkowitz
CC BY-SA 3.0
2015-04-12T20:01:43.830Z
2015-04-12T20:01:43.830Z
Marion Newlevant
<p>This solution requires a little bit of set-up code, but it will keep all your HTML dry. Set up arrays with the values from your block fields. Loop through one of the arrays and use <code>loop.index0</code> to get the values you need in your HTML.</p>

<pre><code>{% set names = [block.bioName1, block.bioName2, ...] %}
{% set titles = [block.bioTitle1, block.bioTitle2, ...] %}
{% set assets = [block.bioImage1, block.bioImage2, ...] %}
{% set texts = [block.bioText1, block.bioText2, ...] %}

{% for field in nameFields %}
    {% if field != '' %}
        &lt;div class="bit"&gt;
            &lt;div class="bio"&gt;
                {% for asset in assets[loop.index0] %}
                    &lt;img src="{{ asset.url }}" alt="" /&gt;
                {% endfor %}
                &lt;header&gt;
                    &lt;h3&gt;{{ names[loop.index0] }}&lt;/h3&gt;
                    &lt;p&gt;{{ titles[loop.index0] }}&lt;/p&gt;
                &lt;/header&gt;
                {{ block.texts[loop.index0] }}
            &lt;/div&gt;
        &lt;/div&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-04-12T19:48:22.057Z
2015-04-12T19:48:22.057Z
Aaron Berkowitz
<p>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:</p>

<ul>
<li>Bio 1 Name (bioName1)</li>
<li>Bio 1 Image (bioImage1)</li>
<li>Bio 2 Name (bioName2)</li>
<li>Bio 2 Image (bioImage2)</li>
<li>etc. up to 6</li>
</ul>

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

<pre><code>    {% 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 %}
</code></pre>

<p>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 :/ </p>

CC BY-SA 3.0
2015-04-12T17:40:07.327Z
Can you share screenshot your field set up? Would you be able to move the"bio" to its own block type so all the fields inside of a bio do not need to be numbered?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-12T19:16:02.817Z
Hi Aaron - A stand alone block, or a "bios" channel [isn't a good fit for this project](http://craftcms.stackexchange.com/questions/6732/solution-for-matrix-within-matrix-or-more-robust-table-fields). After trying those, we decided on this approach b.c it makes life a lot easier for the site's managers. It's working out fine, just trying to see if the templates can be a touch dry-er. If not, no biggie. Tks.
philzelnar
<templating>
Loop for Similar Fields
# Loop for Similar Fields

(Asked by: username_0 on 2015-04-12)

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 %}
    
        
            {% for asset in block.bioImage1 %}
            
            {% endfor %}
            
                {{ block.bioName1 }}
                {{ block.bioTitle1 }}
            
            {{ block.bioText1 }}
        
    
    {% endif %}

    {% if block.bioName2 %}
    
        
            {% for asset in block.bioImage2 %}
            
            {% endfor %}
            
                {{ block.bioName2 }}
                {{ block.bioTitle2 }}
            
            {{ block.bioText2 }}
        
    
    {% 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 :/

**Tags:** templating

> **Comments:**
> **username_1**: Can you share screenshot your field set up? Would you be able to move the"bio" to its own block type so all the fields inside of a bio do not need to be numbered?  
> **username_0**: Hi Aaron - A stand alone block, or a "bios" channel [isn't a good fit for this project](http://craftcms.stackexchange.com/questions/6732/solution-for-matrix-within-matrix-or-more-robust-table-fields). After trying those, we decided on this approach b.c it makes life a lot easier for the site's managers. It's working out fine, just trying to see if the templates can be a touch dry-er. If not, no biggie. Tks.  

---

## 3 answers

### Answer by username_2 on 2015-04-13. Score: 2 | Accepted answer

You can use the "subscript" variable syntax to add the loop index to your field names automatically. 
http://twig.sensiolabs.org/doc/templates.html#variables
{% for i in 1..6 %}
  {% if block['bioName'~i] %}
    
      
        {% for asset in block['bioImage'~i] %}
          
        {% endfor %}
        
          {{ block['bioName'~i] }}
          {{ block['bioTitle'~i] }}
        
        {{ block['bioText'~i] }}
      
    
  {% endif %}
{% endfor %}

I prefer this to the attribute function because it's a little simpler to look at. But the attribute function will work in some circumstances where dot notation and subscript syntax can't.
This is taken from the Twig docs link above:

When the attribute contains special characters (like - that would be interpreted as the minus operator), use the attribute function instead to access the variable attribute:

{# equivalent to the non-working foo.data-foo #}
{{ attribute(foo, 'data-foo') }}


---

### Answer by username_3 on 2015-04-12. Score: 4

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) %}
    
      
        {% for asset in attribute(block, 'bioImage'~i) %}
          
          ...etc...
        {% endfor %}
      
    
  {% endif %}
{% endfor %}

> **Comments:**
> **username_1**: Way better than my answer! Great idea to use attribute.  

---

### Answer by username_1 on 2015-04-12. Score: 1

This solution requires a little bit of set-up code, but it will keep all your HTML dry. Set up arrays with the values from your block fields. Loop through one of the arrays and use loop.index0 to get the values you need in your HTML.
{% set names = [block.bioName1, block.bioName2, ...] %}
{% set titles = [block.bioTitle1, block.bioTitle2, ...] %}
{% set assets = [block.bioImage1, block.bioImage2, ...] %}
{% set texts = [block.bioText1, block.bioText2, ...] %}

{% for field in nameFields %}
    {% if field != '' %}
        
            
                {% for asset in assets[loop.index0] %}
                    
                {% endfor %}
                
                    {{ names[loop.index0] }}
                    {{ titles[loop.index0] }}
                
                {{ block.texts[loop.index0] }}
            
        
    {% endif %}
{% endfor %}


---

