<p>The <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="nofollow">Twig attribute function</a> solved this for me. Thanks to @tom-bauer</p>

<p>Revised Template Code:</p>

<pre><code>{% set newsSectionIds = craft.entries.section('news').ids() %}
{% set pressSectionIds = craft.entries.section('press').ids() %}
{% set entryIds = newsSectionIds|merge(pressSectionIds) %}
{% set entries = craft.entries.id(entryIds) %}

{% for entry in entries %}

    {% set sectionMatrix = attribute(entry, "entryMatrix"~entry.section) %}

    {% for block in sectionMatrix %}

        {# my matrix code #}

    {% endfor %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-02-24T14:44:53.950Z
You can also use the subscript syntax: `entry["entryMatrix"~entry.section]`.
carlcs
CC BY-SA 3.0
2016-02-24T15:37:41.513Z
Thank you. This works perfectly, and is slightly shorter too.
Andrew F
CC BY-SA 3.0
2016-02-24T14:02:49.703Z
2016-02-24T14:02:49.703Z
Andrew F
<p>I'm trying to merge a field name prefix with entry.section to make a dynamic field handle. I'm guessing I'm setting the sectionMatrix variable incorrectly, causing the code to fail without error. Any tips would be greatly appreciated.</p>

<p>Channels:</p>

<ul>
<li>News</li>
<li>Press</li>
</ul>

<p>Each channel has is own matrix field, with different blocks:</p>

<ul>
<li>entryMatrixNews</li>
<li>entryMatrixPress</li>
</ul>

<p>Template Code:</p>

<pre><code>{% set newsSectionIds = craft.entries.section('news').ids() %}
{% set pressSectionIds = craft.entries.section('press').ids() %}
{% set entryIds = newsSectionIds|merge(pressSectionIds) %}
{% set entries = craft.entries.id(entryIds) %}

{% for entry in entries %}
   {% set sectionMatrix = 'entry.entryMatrix' ~ entry.section %}
   {# sectionMatrix then equals either entry.entryMatrixNews or entry.entryMatrixPress #}

   {% for block in sectionMatrix %}
      {# my matrix code #}
   {% endfor %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-02-23T18:01:27.903Z
Take a look on these two Answers: - http://craftcms.stackexchange.com/questions/12627/parse-a-string-dynamic-in-twig - http://craftcms.stackexchange.com/questions/13336/variable-variables-in-twig
Tom Bauer
CC BY-SA 3.0
2016-02-24T13:51:54.860Z
Thanks Tom! The **attribute() twig function** example suggested by @taylor worked perfectly.
Andrew F
<matrix><fieldtypes>
Is it possible to merge entry parameters to create a dynamic field handle?
# Is it possible to merge entry parameters to create a dynamic field handle?

(Asked by: username_0 on 2016-02-23)

I'm trying to merge a field name prefix with entry.section to make a dynamic field handle. I'm guessing I'm setting the sectionMatrix variable incorrectly, causing the code to fail without error. Any tips would be greatly appreciated.
Channels:

News
Press

Each channel has is own matrix field, with different blocks:

entryMatrixNews
entryMatrixPress

Template Code:
{% set newsSectionIds = craft.entries.section('news').ids() %}
{% set pressSectionIds = craft.entries.section('press').ids() %}
{% set entryIds = newsSectionIds|merge(pressSectionIds) %}
{% set entries = craft.entries.id(entryIds) %}

{% for entry in entries %}
   {% set sectionMatrix = 'entry.entryMatrix' ~ entry.section %}
   {# sectionMatrix then equals either entry.entryMatrixNews or entry.entryMatrixPress #}

   {% for block in sectionMatrix %}
      {# my matrix code #}
   {% endfor %}

{% endfor %}

**Tags:** matrix, fieldtypes
> **Comments:**
> **username_1**: Take a look on these two Answers: - http://craftcms.stackexchange.com/questions/12627/parse-a-string-dynamic-in-twig - http://craftcms.stackexchange.com/questions/13336/variable-variables-in-twig  
> **username_0**: Thanks Tom! The **attribute() twig function** example suggested by @taylor worked perfectly.  

---

## 1 answers

### Answer by username_0 on 2016-02-24. Score: 2

The Twig attribute function solved this for me. Thanks to @tom-bauer
Revised Template Code:
{% set newsSectionIds = craft.entries.section('news').ids() %}
{% set pressSectionIds = craft.entries.section('press').ids() %}
{% set entryIds = newsSectionIds|merge(pressSectionIds) %}
{% set entries = craft.entries.id(entryIds) %}

{% for entry in entries %}

    {% set sectionMatrix = attribute(entry, "entryMatrix"~entry.section) %}

    {% for block in sectionMatrix %}

        {# my matrix code #}

    {% endfor %}

{% endfor %}

> **Comments:**
> **username_2**: You can also use the subscript syntax: `entry["entryMatrix"~entry.section]`.  
> **username_0**: Thank you. This works perfectly, and is slightly shorter too.  

---

