1

I have an entity that has an items property that is an array of item entities. the item entity has id and name properties.

what I want to do is to get entity.items and display all name properties, separated by commas.

the way I have it now:

<tr>
        <th>Items</th>
        <td>
            {% for item in entity.items %}
                {{ item.name }}
            {% endfor %}
        </td>
</tr>

But it is not separated by commas. I tried the Join filter, but I can't find a way to use it in this situation, since I have an array of objects.

Heitor
  • 145
  • 2
  • 11

1 Answers1

5

You can combine twig syntax with regular HTML. The {%%} markup indicates tags, telling the twig that there's some rendering logic, but you don't need to write strictly twig syntax inside the tags. So:

{% for item in entity.items %}
    {{ item.name }}{% if not loop.last %}, {% endif %}
{% endfor %}

will work just fine

Teo.sk
  • 2,619
  • 5
  • 25
  • 30