1

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").

peace_love
  • 6,229
  • 11
  • 69
  • 157

1 Answers1

1

If you are able to change the column array to something like 2 => 'type.name', you can use the following snippet to read out nested data:

{% for value in data %}
    {% for column in columns %}
        {% set output = value %}
        {% for method in column|split('.') if method != '' %}
            {% set output = attribute(output, method) | default('') %}
        {% endfor %}
        {{ output }}
    {% endfor %}
{% endfor %}

demo

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • I will check it now! – peace_love Feb 04 '19 at 10:04
  • Yes, unfortunately I get the error message: `An exception has been thrown during the rendering of a template ("Warning: explode() expects parameter 2 to be string, object given").` – peace_love Feb 04 '19 at 10:50
  • 1
    I'm assuming you have your arrays the other way around, so you would need to split `value` and not `v`. I'm using the `data` array as first array and the columns as second – DarkBee Feb 04 '19 at 11:01
  • I gues because `var {{ value }}` is now inside a second loop. Possible? – peace_love Feb 04 '19 at 11:21
  • 1
    Don't forget you need to reverse the `output` variable as well. `{% set output = v %}` - [source](https://twigfiddle.com/qk2z4y) – DarkBee Feb 04 '19 at 11:21
  • Please edit your question with the updated code. It's unbearable to read code in the comment section – DarkBee Feb 04 '19 at 11:37
  • Ah, I think I know, because `value` is now `type.name`. It has to be `type` here `var {{ value }}`... – peace_love Feb 04 '19 at 11:48
  • oh my god, was a stupid mistake. finally found it. `var {{ value | split('.') | first }} = "{{ output }}"` was the solution – peace_love Feb 04 '19 at 13:23