Iván
Iván

Reputation: 67

How to display an attribute that is an array of objects in twig

I want to show a property that is in an array of objects.

When I try to show it in php it works. It is like this.

foreach($elements as $element){

echo 'Order ID ['.$element->getOrders()->getId().']</br>'; 
echo 'Show element ['.$element->getId().']</br>';
echo 'Name ['.$element->getName().']</br>';
echo 'Type ['.$element->getType().']</br>';

}

But I don't know how to do that in twig.

First I send this to twig...

return array(
     'elements' => $elements,
);

and in twig I try to show like this...

<ul>
   <li>OrderID // ElementId // Name // Type </li>
   {% for element in elements %}
   <li>{{ attribute(element, '???')}} // {{ attribute(element, 'id')}} // {{ attribute(element, 'name')}} // {{ attribute(element, 'type')}} </li>
   {% endfor %}
</ul>

Then my problem is how to show this OrderID. What I need to do in attribute or other function to show this.

Upvotes: 1

Views: 2282

Answers (1)

MeuhMeuh
MeuhMeuh

Reputation: 885

You can use :

{{ element.yourAttribute }}

For your orderId, if you have a ManyToOne or a OneToOne relationship, it would be :

{{ element.order.id }}

If it's a OneToMany or a ManyToMany, you should consider going through each order and get your ids. For this, check this page ;-).

{% for order in element.orders %}
    {{ order.id }}
{% endfor %}

Upvotes: 1

Related Questions