Answers.Body: <p>I don't think your approach makes sense but you can use twigs <a href="https://twig.symfony.com/doc/2.x/functions/attribute.html" rel="nofollow noreferrer">Attribute</a> function</p>
  
  <pre><code>{% for block in attribute(sidebar, entry.section.handle ~"Sidebar") %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-05T23:45:52.250Z
Answers.Text: Thanks. This worked: {% set sidebar = listingSettings[entry.section.handle ~"Sidebar"] %}
Answers.UserDisplayName: artmem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-05T23:50:08.617Z
Answers.Text: If you want, we can talk in the chat about your Craft structure. It's not meant to be abused that way and there are many better, cleaner and faster approaches. That are much easier to handle and to extend in the future
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-06T19:40:26.340Z
Answers.Text: Thanks. I appreciate that. Always interested in learning new/better ways to do things.
Answers.UserDisplayName: artmem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-05T19:28:42.967Z
Answers.LastActivityDate: 2018-03-05T19:28:42.967Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm using a single template to display entries from several structures. Each structure has a cooresponding global field that is an entries field. I use this to display related entries as a sidebar. These fields are named sidebars.[structure handle]Sidebar, so fore example my blog sidebar global field would be sidebar.blogSidebar.</p>
  
  <p>Coding the sidebar manually works fine, like this:</p>
  
  <pre><code>{% for block in sidebars.blogSidebar %}
      ...
  {% endfor %}
  </code></pre>
  
  <p>But I'm trying to dynamically set the related entries field, and it isn't working:</p>
  
  <pre><code>{% set sidebar = "sidebars." ~ entry.section.handle ~"Sidebar" %}
  {% for block in sidebar %}
      ...
  {% endfor %}
  </code></pre>
  
  <p>This code doesn't output anything. If i just output {{ sidebar }}, it shows what I expect, ex: sidebars.blogSidebar. </p>
  
  <p>And if I manually add it to the for loop {% for block in sidebars.blogSidebar %}, it works as expected.</p>
  
  <p>Is there a different way to concatenate the name to make this work?</p>
  
Tags: <templating><variable>
Title: Specify a field dynamically using section handle
ThreadText: # Specify a field dynamically using section handle
  
  (Asked by: username_0 on 2018-03-05)
  
  I'm using a single template to display entries from several structures. Each structure has a cooresponding global field that is an entries field. I use this to display related entries as a sidebar. These fields are named sidebars.[structure handle]Sidebar, so fore example my blog sidebar global field would be sidebar.blogSidebar.
  
  Coding the sidebar manually works fine, like this:
  
  ```
  {% for block in sidebars.blogSidebar %}
      ...
  {% endfor %}
  
  ```
  
  But I'm trying to dynamically set the related entries field, and it isn't working:
  
  ```
  {% set sidebar = "sidebars." ~ entry.section.handle ~"Sidebar" %}
  {% for block in sidebar %}
      ...
  {% endfor %}
  
  ```
  
  This code doesn't output anything. If i just output {{ sidebar }}, it shows what I expect, ex: sidebars.blogSidebar.
  
  And if I manually add it to the for loop {% for block in sidebars.blogSidebar %}, it works as expected.
  
  Is there a different way to concatenate the name to make this work?
  
  **Tags:** templating, variable
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-05. Score: 1 | Accepted answer
  
  I don't think your approach makes sense but you can use twigs [Attribute](https://twig.symfony.com/doc/2.x/functions/attribute.html) function
  
  ```
  {% for block in attribute(sidebar, entry.section.handle ~"Sidebar") %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks. This worked: {% set sidebar = listingSettings[entry.section.handle ~"Sidebar"] %}  
  > **username_1**: If you want, we can talk in the chat about your Craft structure. It's not meant to be abused that way and there are many better, cleaner and faster approaches. That are much easier to handle and to extend in the future  
  > **username_0**: Thanks. I appreciate that. Always interested in learning new/better ways to do things.  
  
  ---
  
  