0

I have a page that lists a bunch of project data if it is present.

At the moment the twig structure is doing something like:

{% if entry.partners|length %}

        {% for data in entry.partners.all() %}
    
           {{ data.title }}

        {% endfor %}

{% endif %}

{% if entry.awards|length %}

        {% for data in entry.awards.all() %}
    
           {{ data.title }}

        {% endfor %}

{% endif %}

etc... theres 6 or seven of these possible entry types Im looking at.

I am wondering if I can use a 'switch' setup like this:

{% switch entry.type %}

   {% case "typeA" %}

   {% case "typeB" %}


{% endswitch %}

Following the @james-smith suggestion, I tried this to see if I could winkle out an associated 'client' — which definitely exists  — unfortunately this code below does not work either...

{% switch entry.type.handle %}

   {% case 'clients' %}

        {% for data in entry.clients.all() %}
                    
            {{ data.title }}
                            
        {% endfor %}



{% endswitch %}

thanks for looking!

4 Answers 4

2

Yes, you can. Switch is not native to Twig, but Craft adds its own switch tag to the language, as per the Tags documentation here, which works exactly as you have demonstrated: https://craftcms.com/docs/5.x/reference/twig/tags.html#switch

3
  • Thanks for looking @james-smith the thing is, if I try {% switch entry.type %} {% case "awards" %} for example... it doesnt work. I am NOT using matrix blocks... So Im asking specifically for an example switching entry types without matrix. Basically, what is the right way to target different entry types...?! thanks
    – dezbat
    Commented Dec 10 at 22:54
  • Ah right ok... you didn't actually mention that you'd tried it and it didn't work in your question! Try {{ entry.type.handle }} instead. Commented Dec 11 at 18:52
  • thanks again — unfortunately that doesn't seem to work, see above, I have emended the question to clarify what has already been attempted...
    – dezbat
    Commented 2 days ago
0

If the switch can be made based on entry.type, I might suggest:

  • a separate twig template for each entry type.
  • one template as an entry point.

I assume this is the situation

  • you have 2 entry types: partnersOverview and awardsOverview.
  • partersOverview has a field with handle partners.
  • awardsOverview has a field with handle awards

They would then both use the template: main.twig (or name it whatever you find most suitable).

The overview templates would look like this:

{# templates/entrytypes/partnersOverview.twig #}

{% for data in entry.partners.all() %}
    {{ data.title }}
{% endfor %}
{# templates/entrytypes/awardsOverview.twig #}

{% for data in entry.awards.all() %}
  {{ data.title }}
{% endfor %}

And the "main" template

{# templates/main.twig #}

{% include "entryTypes/" ~ entry.type #}
3
  • Thanks for this suggestion, I do like this approach, the only issue is that I am only after the entry types related to the project entry in question...
    – dezbat
    Commented 2 days ago
  • I would love to know if this approach could work for related entries though :)
    – dezbat
    Commented 2 days ago
  • @geshud — I did try adding a subfolder 'entryTypes' for 'clients' and 'partners', then calling {% include "entryTypes/" ~ entry.relatedTo('clients','partners') %} — which did not work!
    – dezbat
    Commented 2 days ago
0

I figured this out — with a steer from the help here and also Ryan Irelan's article

Seems I needed to 'gather' my entry types before running the switch... I'm sure there's other ways to achieve this, but this is kind of what I envisaged was possible...


{% set projectData = {
    'clients': entry.clients.all(),
    'partners': entry.partners.all()
} %}

{% for key, data in projectData %}

    {% switch key %}

        {% case 'clients' %}

            {% for client in entry.clients.all() %}
                C
            {% endfor %}

        {% case 'partners' %}

            {% for partner in entry.partners.all() %}
                P
            {% endfor %}

    {% endswitch %}

{% endfor %}

1
  • Thanks for clarifying. The confusion in your question stems from the fact that you kept saying "entry types", when really you meant relationship fields. Glad you found your answer though. To optimise this a little, be sure to avoid the double-fetching - any time you use .all() you're making a load of heavy DB queries, so make those calls just once. i.e. for client in data Commented 2 days ago
0

If your projectData is static, I would not use case, in this case.

{% set projectData = {
    'clients': entry.clients.all(),
    'partners': entry.partners.all()
} %}

{% for key, data in projectData %}
    {% switch key %}
        {% case 'clients' %}
            {% for client in entry.clients.all() %}
                C
            {% endfor %}
        {% case 'partners' %}
            {% for partner in entry.partners.all() %}
                P
            {% endfor %}
    {% endswitch %}
{% endfor %}

All this does is (and this looks more readable):

{% for client in entry.clients.all() %}
  C
{% endfor %}

{% for partner in entry.partners.all() %}
  P
{% endfor %}

If you do want to keep it that way and set it up with multiple templates, I'd suggest something like this.

I'm assuming the entries returned by entry.clients.all() are of type client And that those of entry.partners.all() are of type partner. And that you need some sort of card list of clients and followed by a card list of partners.

{% set projectData = {
    'clients': entry.clients.all(),
    'partners': entry.partners.all()
} %}

{% for key, data in projectData %}
  {% if data|length > 0 %}
    {% for currentEntry in data %}

{# Want to keep the switch? #}

      {% switch currentEntry.type %}
        {% case 'client' %}
          {% include 'cards/clientCardList.twig` %}
        {% case 'partner' %}
          {% include 'cards/partnerCardList.twig` %}
      {% endswitch %}

{# I'd rather do it without switch, like so: #}

      {% include 'cards/'~ currentEntry.type ~`CardList.twig` %}

    {% endfor %}
  {% endif %}
{% endfor %}
{% templates/cards/clientCardList.twig %}

{% for client in data %}
  {{ client.title }}
{% endfor %}
{% templates/cards/partnerCardList.twig %}

{% for partner in data %}
  {{ partner.title }}
{% endfor %}

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.