CASE: display values of an object from a collection on one singular line

Got the question just today: I don’t want to list all the directors in my text template. I would rather display them in one singular sentence, like “Tim, Joris, Sven”

Is this possible? Yes it is

If you take this code :

{% for person in period.directors %}
{{ person.name }}
{% endfor %}

it will result in this :

35

With the stripnewlines attribute we can strip all these lines, and put them in one sentence.
Just place the attribute before and after your code to do that :

{% stripnewlines %}
{% for person in period.directors %}
{{ person.name }},  
{% endfor %}
{% endstripnewlines %}

And the result will be like this :

51

Jack it up a little bit with a sentence and to end with an ‘and’ :

The directors of the company are: 
{% for person in period.directors %}
  {% if forloop.last %}
    and
  {% else %}
      {% unless forloop.first %}
        ,
      {% endunless %}
  {% endif %}
  {{ person.name }}
{% endfor %}
{% endstripnewlines %}

to look like this :

38

Credits to @SamVanEenoo for this too :+1:

1 Like