<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>

CC BY-SA 4.0
2018-05-16T07:33:33.837Z
2018-05-16T07:33:33.837Z
Alex Roper
<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>

CC BY-SA 4.0
2018-05-15T20:47:27.797Z
2018-05-15T20:52:38.427Z
2018-05-15T20:52:38.427Z
Jonathan Schofield
<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>

CC BY-SA 4.0
2018-05-15T22:59:14.953Z
If you're looking to nest Matrix fields I highly recommend the [Neo plugin](https://github.com/benjamminf/craft-neo).
Simon Kuran
<templating><variable><array>
Using variable references to fields in a template
# Using variable references to fields in a template

(Asked by: username_0 on 2018-05-15)

I have a situation where Matrix within Matrix 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 %}

    
        {{ matrixBlock.nText|md }}
        {{ matrixBlock.nCite }}
    
{% 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 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 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 %}
    
        {{ matrixBlock['quote' ~ n] }}
        {{ matrixBlock['attribution' ~ n] }}
    
    {% 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 %}
    
        {{ attribute(matrixBlock, 'quote' ~ n) }}
        {{ attribute(matrixBlock, 'attribution' ~ n) }}
    
    {% endif %}
{% endfor %}


---

