Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T07:12:25.360Z
Answers.LastActivityDate: 2018-03-09T07:19:36.780Z
Answers.LastEditDate: 2018-03-09T07:19:36.780Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T02:22:19.043Z
Answers.LastActivityDate: 2018-03-09T02:22:19.043Z
Answers.UserDisplayName: Katie Fritz
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-09T01:55:30.920Z
Comments.Text: Possibly related: https://stackoverflow.com/questions/7378975/how-do-i-access-a-member-in-twig-determined-by-a-variable
Comments.UserDisplayName: Katie Fritz
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-09T01:56:24.663Z
Comments.Text: Or maybe I want to use the Twig `attribute()` function? I'm not sure how to use it.
Comments.UserDisplayName: Katie Fritz
Tags: <templating><field><variable><array><macro>
Title: How can I iterate through an array of field handles for an entry?
ThreadText: # 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 %}
    <section class="col-xl mb-3">
      <h2 id="courseDesign">Course Design</h2>
      {{ entry.courseDesign }}
    </section>
  {% 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 %}
  
    <section class="col-xl mb-3">
      <h2 id="{{ field.handle }}">{{ field.title }}</h2>
      {{ entry[field.handle] }}
    </section>
  
  {% 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" %}
                      <section class="col-xl mb-3">
                          <h2 id="{{ field.handle }}">{{ entry.getFieldValue(field.handle) }}</h2>
                          {{ entry.getFieldValue(field.handle) }}
                      </section>
                  {% case "craft\\fields\\Assets" %}
                      {% set asset = entry.getFieldValue(field.handle).one() %}
                      {% if asset %}
                          <img src="{{ asset.getUrl() }}" alt="{{ asset.title }}" class="" >
                      {% 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
  
  
  ---
  
  