I'm trying to fill a grid, but I don't know how call a object method using a variable. Variable "menus" is my entities, and "itens" is an array with what I wanna show in this grid.
$itens = array(
array('name' => 'id', 'label' => 'Id'),
array('name' => 'parent', 'label' => 'Pai'),
array('name' => 'name', 'label' => 'Nome'),
array('name' => 'route', 'label' => 'Rota'),
array('name' => 'position', 'label' => 'Posição'),
);
Here's my code:
{% for menu in menus %}
<tr>
{% for item in itens %}
<td>{{ attribute(menu, item['name']) }}</td>
{% endfor %}
</tr>
{% endfor %}
I tried too with menu.item['name'], but without success... Any idea?
In Twig templates variables can be accessed using double curly braces notation {{ variableName }} .
Twig and JS code is more tightly knit. You can easily transfer Twig variables into JS. Your code is more portable - you can easily reuse component by just copying a single file to other project.
I got it!!! I did this:
{% for menu in menus %}
<tr>
{% for item in itens %}
{% set method %}{{ item['name'] }}{% endset %}
<td>{{ attribute(menu, method) }}</td>
{% endfor %}
</tr>
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With