Answers.Body: <p>You can also use the <a href="https://twig.symfony.com/doc/2.x/templates.html#variables" rel="nofollow noreferrer">subscript syntax</a> which is slightly more terse. And to iterate over a sequence of numbers, you can use the <code>..</code> operator instead of listing the whole array.</p>
  
  <pre><code>{% for n in 1..6 %}
      {% if matrixBlock['quote' ~ n]|length %}
      &lt;blockquote&gt;
          {{ matrixBlock['quote' ~ n] }}
          &lt;cite&gt;{{ matrixBlock['attribution' ~ n] }}&lt;/cite&gt;
      &lt;/blockquote&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-16T07:33:33.837Z
Answers.LastActivityDate: 2018-05-16T07:33:33.837Z
Answers.UserDisplayName: Alex Roper
Answers.Body: <p>Figured it out:</p>
  
  <pre><code>{% for n in [1, 2, 3, 4, 5, 6] %}
      {% if attribute(matrixBlock, 'quote' ~ n)|length %}
      &lt;blockquote&gt;
          {{ attribute(matrixBlock, 'quote' ~ n) }}
          &lt;cite&gt;{{ attribute(matrixBlock, 'attribution' ~ n) }}&lt;/cite&gt;
      &lt;/blockquote&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-15T20:47:27.797Z
Answers.LastActivityDate: 2018-05-15T20:52:38.427Z
Answers.LastEditDate: 2018-05-15T20:52:38.427Z
Answers.UserDisplayName: Jonathan Schofield
Body: <p>I have a situation where <a href="https://github.com/craftcms/cms/issues/812" rel="nofollow noreferrer">Matrix within Matrix</a> would be ideal, but instead we've ended up with a Matrix row that has pairs of fields up to a maximum of 6:</p>
  
  <pre><code>quote1
  attribution1
  …
  quoteN
  attributionN
  …
  quote6
  attribution6
  </code></pre>
  
  <p>Rather than work through each pair…</p>
  
  <p><code>{% if matrixBlock.quote1|length %} {# quote 1 stuff #} {% endif %}
  …
  {% if matrixBlock.quote6|length %} {# quote 6 stuff #} {% endif %}</code></p>
  
  <p>…I thought I might be able to use a variable reference to numbers 1 to 6:</p>
  
  <pre><code>{% set quotes = [1, 2, 3, 4, 5, 6] %}
  
  {% for n in quotes %}
      {% set nText = 'quote' ~ n %}
      {% set nCite = 'attribution' ~ n %}
  
      &lt;blockquote&gt;
          {{ matrixBlock.nText|md }}
          &lt;cite&gt;{{ matrixBlock.nCite }}&lt;/cite&gt;
      &lt;/blockquote&gt;
  {% endfor %}
  </code></pre>
  
  <p>…but although <code>{{ dump(nText) }}</code> confirms the reference string is being parsed correctly, the string is clearly invalid as a field reference.</p>
  
  <p>Is there a way to get this done to achieve a nice terse template (not the end of the world if I can't)?</p>
  
  <p>I have a sense that Twig's <a href="https://twig.symfony.com/doc/2.x/functions/attribute.html" rel="nofollow noreferrer">attribute</a> function might help, but I can't join the dots.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-15T22:59:14.953Z
Comments.Text: If you're looking to nest Matrix fields I highly recommend the [Neo plugin](https://github.com/benjamminf/craft-neo).
Comments.UserDisplayName: Simon Kuran
Tags: <templating><variable><array>
Title: Using variable references to fields in a template
ThreadText: # Using variable references to fields in a template
  
  (Asked by: username_0 on 2018-05-15)
  
  I have a situation where [Matrix within Matrix](https://github.com/craftcms/cms/issues/812) would be ideal, but instead we've ended up with a Matrix row that has pairs of fields up to a maximum of 6:
  
  ```
  quote1
  attribution1
  …
  quoteN
  attributionN
  …
  quote6
  attribution6
  
  ```
  
  Rather than work through each pair…
  
  `{% if matrixBlock.quote1|length %} {# quote 1 stuff #} {% endif %}
  …
  {% if matrixBlock.quote6|length %} {# quote 6 stuff #} {% endif %}`
  
  …I thought I might be able to use a variable reference to numbers 1 to 6:
  
  ```
  {% set quotes = [1, 2, 3, 4, 5, 6] %}
  
  {% for n in quotes %}
      {% set nText = 'quote' ~ n %}
      {% set nCite = 'attribution' ~ n %}
  
      <blockquote>
          {{ matrixBlock.nText|md }}
          <cite>{{ matrixBlock.nCite }}</cite>
      </blockquote>
  {% endfor %}
  
  ```
  
  …but although `{{ dump(nText) }}` confirms the reference string is being parsed correctly, the string is clearly invalid as a field reference.
  
  Is there a way to get this done to achieve a nice terse template (not the end of the world if I can't)?
  
  I have a sense that Twig's [attribute](https://twig.symfony.com/doc/2.x/functions/attribute.html) function might help, but I can't join the dots.
  
  **Tags:** templating, variable, array
  
  > **Comments:**
  > **username_1**: If you're looking to nest Matrix fields I highly recommend the [Neo plugin](https://github.com/benjamminf/craft-neo).  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2018-05-16. Score: 1
  
  You can also use the [subscript syntax](https://twig.symfony.com/doc/2.x/templates.html#variables) which is slightly more terse. And to iterate over a sequence of numbers, you can use the `..` operator instead of listing the whole array.
  
  ```
  {% for n in 1..6 %}
      {% if matrixBlock['quote' ~ n]|length %}
      <blockquote>
          {{ matrixBlock['quote' ~ n] }}
          <cite>{{ matrixBlock['attribution' ~ n] }}</cite>
      </blockquote>
      {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  ### Answer by username_0 on 2018-05-15. Score: 1
  
  Figured it out:
  
  ```
  {% for n in [1, 2, 3, 4, 5, 6] %}
      {% if attribute(matrixBlock, 'quote' ~ n)|length %}
      <blockquote>
          {{ attribute(matrixBlock, 'quote' ~ n) }}
          <cite>{{ attribute(matrixBlock, 'attribution' ~ n) }}</cite>
      </blockquote>
      {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  