Symfony Twig Call Object Method in Array using Variable as Array Key

I have an an array of objects that I’ve passed into a Symfony Twig template. I actually have several hashes of objects that I’m passing in that are all keyed the same way. When looping one of these hashes, I need to pull in other bits of data from the other hashes. So I needed to be able to fetch an object from one of the other hashes and call one of its methods.

To do it with Twig was easy.

Imagine there’s a hash of Transaction objects keyed with a collatorId and you want to check if it’s been processed or not while looping through a hash of Requests, also keyed with the same collatorIds.

{% for request in requests %}    
  {% if transactions[request.collatorId].processed != TRUE %}
  NOT PROCESSED
  {% else %}
  PROCESSED
  {% endif %}
{% endfor %}

That will call the processed method for the transaction with a collatorId of request.collatorId.

Share Button