I am accessing dynamic data from an array:
{% for key, value in columns_arr %}
{% for k,v in group %}
var {{ value }} = "{{ attribute(v, value) }}";
{% endfor %}
{% endfor %}
This is working well for name
and id
(see below). In the example of name
the attribute ...
{{ attribute(v, value) }}
is replacing:
{{ v.name }}
and in the example of id
it is replacing ...
{{ v.id }}
But this is not working with type
because here I actually need to replace:
{{ v.type.name }}
So my question is, how would this look like in the attribute function?
I tried {{ attribute(v.name, value) }}
but I get the error
Impossible to access an attribute ("type") on a string variable ("ID").
group:
array:4 [▼
0 => Fields {#7444 ▼
-id: 1
-name: "ID"
-unique_id: "6ab8c870ed"
-productgroup: PersistentCollection {#7448 ▶}
-type: Type {#7525 ▼
+__isInitialized__: true
-id: 2
-name: "hidden"
-unique_id: "5e1086c862"
-label: "hidden"
…2
}
}
1 => Fields {#7526 ▶}
2 => Fields {#7530 ▶}
3 => Fields {#7534 ▶}
]
columns_arr:
array:3 [▼
0 => "id"
1 => "name"
2 => "type"
]
My approach according to this question:
How to check a multidimensional Twig array for values?
{% for key, value in columns_arr %}
{% for k,v in group %}
{% for k1,v1 in v %}
var {{ value }} = "{{ attribute(name, v1) }}";
{% endfor %}
{% endfor %}
{% endfor %}
But this gives me an error, My page is not loading anymore.
Another approach is this:
{{ attribute(v, [value.name]) }}
But I get the error:
Impossible to access an attribute ("name") on a string variable ("type").