[solved] Showing one contributor with several roles – without duplicating contributor

My use case is as follows: For a particular episode, I have a contributor who does both editing and presenting.

I have now added the contributor twice, once for each role. So far, I use a {% for contributor in episode.contributors %} loop, which of course ends up giving me the contributor twice. This looks too cluttered, especially with more roles I would like to add. This leads me to my questions:

For a particular episode, is there a way to store multiple roles for a particular contributor without adding the contributor twice, three times etc.?

Alternatively, if the contributor is stored twice (once with the role “editor” and once with the role “presenter”), is there a way to elegantly extract/present all the roles of a particular person with the template language? I would be grateful for a tip to get a result looking something like this:

  • Contributor1 (presenting)
  • Contributor2 (editing, presenting).

You can’t store multiple roles per entry (intentionally).

It’s possible to reduce the data to show contributors which each assigned role using Podlove Templates. But I don’t have a solution handy right now, maybe @ericteubert can give you a hint?

Correct, the only way to do it is to add the same contributor multiple times with different roles. To make this work in Twig is a bit cumbersome but possible. This template works:

{# remember which contributors we printed already so we won't have duplicates #}
{% set contributorsDone = [] %}

{% for contributor in episode.contributors %}

  {# skip if we printed this contributor already #}
  {% if not (contributor.name in contributorsDone) %}

    {# loop over all contributors and remember all roles for current contributor #}
    {% set roles = [] %}
    {% for c in episode.contributors %}
        {% if c.name == contributor.name %}
            {% set roles = roles|merge([c.role]) %}
        {% endif %}
    {% endfor %}
    {# mark this contributor as done #}
    {% set contributorsDone = contributorsDone|merge([contributor.name]) %}

          {{ contributor.name }} ({{ roles|join(", ") }})
  {% endif %}
{% endfor %}

Thank you for the swift and comprehensive replies! @ericteubert: Perfect, this was exactly what I was looking for and it works.

Out of curiosity, is there a reason why there is only one role per entry? Is it for the sake of simplicity?

Yes :slight_smile: And I would venture to argue that it solves at least 90% of use cases. Assigning a single role or just the main role is enough for most. As far as I can remember, you are the first one asking for multiple assignments :wink:

Is it possible to break a loop earlier, if e.g. the entry was already found an no other loop-iterations are necessary?

From the Twig documentation:

Unlike in PHP, it’s not possible to break or continue in a loop.