1

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.

2
  • Have you tried {{ element.name }} or {{ element.orders.id }} ? Commented Jul 30, 2015 at 7:50
  • attribute(array, 'elementName') is used when the elementName is dynamic, because you can't call array.dynamicElementName. Commented Jul 30, 2015 at 10:44

1 Answer 1

1

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 %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.