<p>Please try the following code:</p>

<pre><code>{% set dynamicMenu = menutype~".menuGlobal" %}

{% for block in dynamicMenu %}
.
.
.
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-12-20T15:09:11.840Z
2017-12-20T15:09:11.840Z
hashtagerrors
<p>I believe you're looking for Twig's <a href="https://twig.symfony.com/doc/2.x/functions/attribute.html" rel="nofollow noreferrer">attribute</a> method.</p>

<pre><code>{% for block in attribute(menutype, menuGlobal) %}
</code></pre>

<p>It allows you to get dynamic attributes off of a variable.</p>

CC BY-SA 3.0
2017-12-20T19:33:47.887Z
2017-12-20T19:33:47.887Z
Brad Bell
<pre><code>the var menutype is set to {{ menutype }}

{% for block in menutype.menuGlobal %}
</code></pre>

<p>I'm trying to pass the result of {{ menutype }} into the for loop to return a dynamic menu, but the loop doesn't run. Hardcoding the string works, so the loop is fine, just appears not to recognise the variable output in the for statement.</p>

CC BY-SA 3.0
2017-12-20T13:50:49.043Z
What is the data you have assigned to {{ menutype }}? Is it a Navee object? Is it a custom field? Is it a structure? Give a bit more info here on what that data is and i'll be able to help. Also, do you have devmode on?
John
CC BY-SA 3.0
2017-12-21T15:05:05.647Z
Could you post more of your code? What does the variable `menutype` look like? Does it have `menuGlobal` as a child in it?
Cavell Blood
<templating>
Use twig Var in for loop
# Use twig Var in for loop

(Asked by: username_0 on 2017-12-20)

the var menutype is set to {{ menutype }}

{% for block in menutype.menuGlobal %}

I'm trying to pass the result of {{ menutype }} into the for loop to return a dynamic menu, but the loop doesn't run. Hardcoding the string works, so the loop is fine, just appears not to recognise the variable output in the for statement.

**Tags:** templating
> **Comments:**
> **username_1**: What is the data you have assigned to {{ menutype }}? Is it a Navee object? Is it a custom field? Is it a structure? Give a bit more info here on what that data is and i'll be able to help. Also, do you have devmode on?  
> **username_2**: Could you post more of your code? What does the variable `menutype` look like? Does it have `menuGlobal` as a child in it?  

---

## 2 answers

### Answer by username_3 on 2017-12-20. Score: 1

Please try the following code:
{% set dynamicMenu = menutype~".menuGlobal" %}

{% for block in dynamicMenu %}
.
.
.
{% endfor %}


---

### Answer by username_4 on 2017-12-20. Score: 0

I believe you're looking for Twig's attribute method.
{% for block in attribute(menutype, menuGlobal) %}

It allows you to get dynamic attributes off of a variable.


---

