Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-13T09:30:16.050Z
Answers.LastActivityDate: 2015-04-13T09:30:16.050Z
Answers.UserDisplayName: Alex Roper
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-13T02:18:29.233Z
Answers.Text: Way better than my answer! Great idea to use attribute.
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-12T20:01:43.830Z
Answers.LastActivityDate: 2015-04-12T20:01:43.830Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-12T19:48:22.057Z
Answers.LastActivityDate: 2015-04-12T19:48:22.057Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-12T17:40:07.327Z
Comments.Text: 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?
Comments.UserDisplayName: Aaron Berkowitz
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-12T19:16:02.817Z
Comments.Text: 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.
Comments.UserDisplayName: philzelnar
Tags: <templating>
Title: Loop for Similar Fields
ThreadText: # 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 %}
      <div class="bit">
          <div class="bio">
              {% for asset in block.bioImage1 %}
              <img src="{{ asset.url }}" alt="" />
              {% endfor %}
              <header>
                  <h3>{{ block.bioName1 }}</h3>
                  <p>{{ block.bioTitle1 }}</p>
              </header>
              {{ block.bioText1 }}
          </div>
      </div>
      {% endif %}
  
      {% if block.bioName2 %}
      <div class="bit">
          <div class="bio">
              {% for asset in block.bioImage2 %}
              <img src="{{ asset.url }}" alt="" />
              {% endfor %}
              <header>
                  <h3>{{ block.bioName2 }}</h3>
                  <p>{{ block.bioTitle2 }}</p>
              </header>
              {{ block.bioText2 }}
          </div>
      </div>
      {% 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 username_1 - 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] %}
      <div class="bit">
        <div class="bio">
          {% for asset in block['bioImage'~i] %}
            <img src="{{ asset.url }}" alt="" />
          {% endfor %}
          <header>
            <h3>{{ block['bioName'~i] }}</h3>
            <p>{{ block['bioTitle'~i] }}</p>
          </header>
          {{ block['bioText'~i] }}
        </div>
      </div>
    {% 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](http://twig.sensiolabs.org/doc/functions/attribute.html) 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) %}
      <div class="bit">
        <div class="bio">
          {% for asset in attribute(block, 'bioImage'~i) %}
            <img src="{{asset.url}}"/>
            ...etc...
          {% endfor %}
        </div>
      </div>
    {% 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 != '' %}
          <div class="bit">
              <div class="bio">
                  {% for asset in assets[loop.index0] %}
                      <img src="{{ asset.url }}" alt="" />
                  {% endfor %}
                  <header>
                      <h3>{{ names[loop.index0] }}</h3>
                      <p>{{ titles[loop.index0] }}</p>
                  </header>
                  {{ block.texts[loop.index0] }}
              </div>
          </div>
      {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  