<p>Try using an array syntax instead of the object syntax:</p>

<pre><code>{% set string = entry['fieldname' ~ 1] %}
</code></pre>

CC BY-SA 3.0
2014-09-29T00:55:44.220Z
2014-09-29T00:55:44.220Z
Lindsey D
<p>I'm creating a macro I'd like to reuse multiple times but the only thing that changes is the field name (matrix). Here's an example:</p>

<pre><code>{% for block in entry.fieldname1 %}
&lt;h3&gt;{{ block.title }}&lt;/h3&gt;
</code></pre>

<p>I'd like to increment this macro so I don't rewrite this code snippet over and over again but when I try and change <strong>entry.fieldname1</strong> to a string it's no<code>{% set string = entry.fieldname ~ 1 %}</code> it is no longer recognized inside the for loop.</p>

<p>Any ideas? </p>

<templating><macro>
Field name in reusable macro
# Field name in reusable macro

(Asked by: username_0 on 2014-09-29)

I'm creating a macro I'd like to reuse multiple times but the only thing that changes is the field name (matrix). Here's an example:
{% for block in entry.fieldname1 %}
{{ block.title }}

I'd like to increment this macro so I don't rewrite this code snippet over and over again but when I try and change entry.fieldname1 to a string it's no{% set string = entry.fieldname ~ 1 %} it is no longer recognized inside the for loop.
Any ideas?

**Tags:** templating, macro

---

## 1 answers

### Answer by username_1 on 2014-09-29. Score: 3 | Accepted answer

Try using an array syntax instead of the object syntax:
{% set string = entry['fieldname' ~ 1] %}


---

