<p>Just as an additional note: instead of creating your fields array in your template you can use Craft functions</p>

<pre><code>{% set layout = entry.getFieldLayout() %}
{#
    you can either output all tabs or check for a specific tab where all
    your "frontend" form/output fields live
    so you could create a "frontend" tab in your fieldLayoutDesigner
    If you just want to display all fields do
    for field in layout.getFields() directly
#}
{% for tab in layout.tabs %}
    {# you can either output all#}
    {{ tab.name }}
    {% if tab.name == "Frontend" %}
        {% for field in tab.getFields %}
            {% switch field.className() %}
                {# different templates for different field types #}
                {% case "craft\\fields\\PlainText" %}
                    &lt;section class="col-xl mb-3"&gt;
                        &lt;h2 id="{{ field.handle }}"&gt;{{ entry.getFieldValue(field.handle) }}&lt;/h2&gt;
                        {{ entry.getFieldValue(field.handle) }}
                    &lt;/section&gt;
                {% case "craft\\fields\\Assets" %}
                    {% set asset = entry.getFieldValue(field.handle).one() %}
                    {% if asset %}
                        &lt;img src="{{ asset.getUrl() }}" alt="{{ asset.title }}" class="" &gt;
                    {% endif %}
            {% endswitch %}

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

<p>Edit you can also list all fields for your frontend in a certain group and do</p>

<pre><code>{% for field in layout.getFields %}
    {% if field.groupId == X %}

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

<p>In order to group all your fields so you won't need that much work and it's easier to change things via CP</p>

<p>In order to gain access to field values by string you can/should use the <code>Element-&gt;getFieldByValue($handle)</code> function. Twigs <code>element-&gt;handle</code> could be explained as a "slower" redirect to that function but it fetches the field layout first and checks if the field is actually in the layout and if the field exists. In the end, the function above is executed so you can use it directly as well</p>

CC BY-SA 3.0
2018-03-09T07:12:25.360Z
2018-03-09T07:19:36.780Z
2018-03-09T07:19:36.780Z
Robin Schambach
<p>I figured it out! I ditched the macro and just included the following in my template:</p>

<pre><code>{% for field in courseFields %}

  &lt;section class="col-xl mb-3"&gt;
    &lt;h2 id="{{ field.handle }}"&gt;{{ field.title }}&lt;/h2&gt;
    {{ entry[field.handle] }}
  &lt;/section&gt;

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

CC BY-SA 3.0
2018-03-09T02:22:19.043Z
2018-03-09T02:22:19.043Z
Katie Fritz
<p><strong>Background:</strong></p>

<p>One of my templates has up to 6 fields that should each go into the same markup. Here's an example with a <code>courseDesign</code> field:</p>

<pre><code>{% if entry.courseDesign|length %}
  &lt;section class="col-xl mb-3"&gt;
    &lt;h2 id="courseDesign"&gt;Course Design&lt;/h2&gt;
    {{ entry.courseDesign }}
  &lt;/section&gt;
{% endif %}
</code></pre>

<p>This worked fine when I wrote each section out like this. However, once I also added a table of contents/nav, I decided to refactor. </p>

<p>I now have a <code>courseFields</code> array, and I'm adding each field to it if it exists:</p>

<pre><code>{% set courseFields = {} %}
{% if entry.courseDesign %}
  {% set courseFields = courseFields|merge({ "courseDesign": { handle: "courseDesign", title: "Course Design" } }) %}
{% endif %}
</code></pre>

<p>My navigation macro works perfectly, because it's only interacting with the objects in the <code>courseFields</code> array.</p>

<p><strong>My problem:</strong> </p>

<p>I can't figure out how to iterate through my fields and get the actual content from them using a macro. </p>

<p>If I use {{ entry.handle }} in my macro, even when <code>handle</code> is a valid field handle, I get the runtime error <code>Variable "entry" does not exist.</code> </p>

<p>I also tried: </p>

<pre><code>{% set courseField = "entry." ~handle %}
{{ courseField }}
</code></pre>

<p>but that produces a string: "entry.courseDesign". </p>

<p><strong>How do I access <code>entry</code> from my macro? Should I be going about this a different way?</strong></p>

CC BY-SA 3.0
2018-03-09T01:55:30.920Z
Possibly related: https://stackoverflow.com/questions/7378975/how-do-i-access-a-member-in-twig-determined-by-a-variable
Katie Fritz
CC BY-SA 3.0
2018-03-09T01:56:24.663Z
Or maybe I want to use the Twig `attribute()` function? I'm not sure how to use it.
Katie Fritz
<templating><field><variable><array><macro>
How can I iterate through an array of field handles for an entry?
# How can I iterate through an array of field handles for an entry?

(Asked by: username_0 on 2018-03-09)

Background:
One of my templates has up to 6 fields that should each go into the same markup. Here's an example with a courseDesign field:
{% if entry.courseDesign|length %}
  
    Course Design
    {{ entry.courseDesign }}
  
{% endif %}

This worked fine when I wrote each section out like this. However, once I also added a table of contents/nav, I decided to refactor. 
I now have a courseFields array, and I'm adding each field to it if it exists:
{% set courseFields = {} %}
{% if entry.courseDesign %}
  {% set courseFields = courseFields|merge({ "courseDesign": { handle: "courseDesign", title: "Course Design" } }) %}
{% endif %}

My navigation macro works perfectly, because it's only interacting with the objects in the courseFields array.
My problem: 
I can't figure out how to iterate through my fields and get the actual content from them using a macro. 
If I use {{ entry.handle }} in my macro, even when handle is a valid field handle, I get the runtime error Variable "entry" does not exist. 
I also tried: 
{% set courseField = "entry." ~handle %}
{{ courseField }}

but that produces a string: "entry.courseDesign". 
How do I access entry from my macro? Should I be going about this a different way?

**Tags:** templating, field, variable, array, macro
> **Comments:**
> **username_0**: Possibly related: https://stackoverflow.com/questions/7378975/how-do-i-access-a-member-in-twig-determined-by-a-variable  
> **username_0**: Or maybe I want to use the Twig `attribute()` function? I'm not sure how to use it.  

---

## 2 answers

### Answer by username_0 on 2018-03-09. Score: 4 | Accepted answer

I figured it out! I ditched the macro and just included the following in my template:
{% for field in courseFields %}

  
    {{ field.title }}
    {{ entry[field.handle] }}
  

{% endfor %}


---

### Answer by username_1 on 2018-03-09. Score: 2

Just as an additional note: instead of creating your fields array in your template you can use Craft functions
{% set layout = entry.getFieldLayout() %}
{#
    you can either output all tabs or check for a specific tab where all
    your "frontend" form/output fields live
    so you could create a "frontend" tab in your fieldLayoutDesigner
    If you just want to display all fields do
    for field in layout.getFields() directly
#}
{% for tab in layout.tabs %}
    {# you can either output all#}
    {{ tab.name }}
    {% if tab.name == "Frontend" %}
        {% for field in tab.getFields %}
            {% switch field.className() %}
                {# different templates for different field types #}
                {% case "craft\\fields\\PlainText" %}
                    
                        {{ entry.getFieldValue(field.handle) }}
                        {{ entry.getFieldValue(field.handle) }}
                    
                {% case "craft\\fields\\Assets" %}
                    {% set asset = entry.getFieldValue(field.handle).one() %}
                    {% if asset %}
                        
                    {% endif %}
            {% endswitch %}

        {% endfor %}
    {% endif %}
{% endfor %}

Edit you can also list all fields for your frontend in a certain group and do
{% for field in layout.getFields %}
    {% if field.groupId == X %}

    {% endif %}
{% endfor %}

In order to group all your fields so you won't need that much work and it's easier to change things via CP
In order to gain access to field values by string you can/should use the Element->getFieldByValue($handle) function. Twigs element->handle could be explained as a "slower" redirect to that function but it fetches the field layout first and checks if the field is actually in the layout and if the field exists. In the end, the function above is executed so you can use it directly as well


---

