<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>

CC BY-SA 3.0
2018-03-05T23:45:52.250Z
Thanks. This worked: {% set sidebar = listingSettings[entry.section.handle ~"Sidebar"] %}
artmem
CC BY-SA 3.0
2018-03-05T23:50:08.617Z
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
Robin Schambach
CC BY-SA 3.0
2018-03-06T19:40:26.340Z
Thanks. I appreciate that. Always interested in learning new/better ways to do things.
artmem
CC BY-SA 3.0
2018-03-05T19:28:42.967Z
2018-03-05T19:28:42.967Z
Robin Schambach
<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>

<templating><variable>
Specify a field dynamically using section handle
# 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 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.  

---

