Fab
Fab

Reputation: 668

Symfony twig use keys in for loops

Does somebody know an other way to do that with twig, because it returns me an error :/

{% for key, conversation in conversations %}
    {% set lastMessage = sortedConversations.key %}
    <p>{{ lastMessage }}</p>
{% endfor %}

Here is the error returned : Key "key" for array with keys "0" does not exist

Thanks !

Upvotes: 0

Views: 105

Answers (2)

Emanuel Oster
Emanuel Oster

Reputation: 1296

Try this:

{% for key, conversation in conversations %}
  {% set lastMessage = sortedConversations[key] %}
  <p>{{ lastMessage }}</p>
{% endfor %}

Note the brackets around key. This way, twig should notice key as a variable and not as a simple string.

Upvotes: 1

doydoy44
doydoy44

Reputation: 5772

I'm not sure to understand, but may be you can try this:

sortedConversations[key]

instead of

sortedConversations.key

Upvotes: 2

Related Questions