Answers.Body: <p>I figured this out — with a steer from the help here and also <a href="https://craftquest.io/articles/switch-statements-in-twig" rel="nofollow noreferrer">Ryan Irelan's article</a></p>
  <p>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...</p>
  <pre><code>
  {% set projectData = {
      'clients': entry.clients.all(),
      'partners': entry.partners.all()
  } %}
  
  {% for key, data in projectData %}
  
      {% switch key %}
  
          {% case 'clients' %}
  
              {% for client in data %}
                  C
              {% endfor %}
  
          {% case 'partners' %}
  
              {% for partner in data %}
                  P
              {% endfor %}
  
      {% endswitch %}
  
  {% endfor %}
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-12T09:06:23.073Z
Answers.Text: 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`
Answers.UserDisplayName: James Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-15T17:45:20.830Z
Answers.Text: Thanks @JamesSmith! Have edited this reply accordingly...
Answers.UserDisplayName: dezbat
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-11T23:02:37.780Z
Answers.LastActivityDate: 2024-12-15T17:45:57.237Z
Answers.LastEditDate: 2024-12-15T17:45:57.237Z
Answers.UserDisplayName: dezbat
Answers.Body: <p>Yes, you can. Switch is not native to Twig, but Craft adds its own <code>switch</code> tag to the language, as per the Tags documentation here, which works exactly as you have demonstrated: <a href="https://craftcms.com/docs/5.x/reference/twig/tags.html#switch" rel="nofollow noreferrer">https://craftcms.com/docs/5.x/reference/twig/tags.html#switch</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-10T22:54:54.270Z
Answers.Text: 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
Answers.UserDisplayName: dezbat
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-11T18:52:39.267Z
Answers.Text: 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.
Answers.UserDisplayName: James Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-11T22:20:58.363Z
Answers.Text: thanks again — unfortunately that doesn't seem to work, see above, I have emended the question to clarify what has already been attempted...
Answers.UserDisplayName: dezbat
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-10T13:23:41.127Z
Answers.LastActivityDate: 2024-12-10T13:23:41.127Z
Answers.UserDisplayName: James Smith
Answers.Body: <p>If your <code>projectData</code> is static, I would not use <code>case</code>, in this case.</p>
  <pre><code>{% 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 %}
  </code></pre>
  <p>All this does is (and this looks more readable):</p>
  <pre><code>{% for client in entry.clients.all() %}
    C
  {% endfor %}
  
  {% for partner in entry.partners.all() %}
    P
  {% endfor %}
  </code></pre>
  <p>If you do want to keep it that way and set it up with multiple templates, I'd suggest something like this.</p>
  <p>I'm assuming the entries returned by <code>entry.clients.all()</code> are of type <code>client</code>
  And that those of <code>entry.partners.all()</code> are of type <code>partner</code>.
  And that you need some sort of card list of clients and followed by a card list of partners.</p>
  <pre><code>{% set projectData = {
      'clients': entry.clients.all(),
      'partners': entry.partners.all()
  } %}
  
  {% for key, data in projectData %}
    {% if data|length &gt; 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 %}
  </code></pre>
  <pre><code>{% templates/cards/clientCardList.twig %}
  
  {% for client in data %}
    {{ client.title }}
  {% endfor %}
  </code></pre>
  <pre><code>{% templates/cards/partnerCardList.twig %}
  
  {% for partner in data %}
    {{ partner.title }}
  {% endfor %}
  </code></pre>
  <p>Good luck!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-14T08:46:30.347Z
Answers.LastActivityDate: 2024-12-14T08:46:30.347Z
Answers.UserDisplayName: geschud
Answers.Body: <p>If the switch can be made based on <code>entry.type</code>, I might suggest:</p>
  <ul>
  <li>a separate twig template for each entry type.</li>
  <li>one template as an entry point.</li>
  </ul>
  <p>I assume this is the situation</p>
  <ul>
  <li>you have 2 entry types: <code>partnersOverview</code> and <code>awardsOverview</code>.</li>
  <li><code>partersOverview</code> has a field with handle <code>partners</code>.</li>
  <li><code>awardsOverview</code> has a field with handle <code>awards</code></li>
  </ul>
  <p>They would then both use the template: <code>main.twig</code> (or name it whatever you find most suitable).</p>
  <p>The overview templates would look like this:</p>
  <pre class="lang-none prettyprint-override"><code>{# templates/entrytypes/partnersOverview.twig #}
  
  {% for data in entry.partners.all() %}
      {{ data.title }}
  {% endfor %}
  </code></pre>
  <pre class="lang-none prettyprint-override"><code>{# templates/entrytypes/awardsOverview.twig #}
  
  {% for data in entry.awards.all() %}
    {{ data.title }}
  {% endfor %}
  </code></pre>
  <p>And the &quot;main&quot; template</p>
  <pre class="lang-none prettyprint-override"><code>{# templates/main.twig #}
  
  {% include &quot;entryTypes/&quot; ~ entry.type #}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-11T23:20:55.997Z
Answers.Text: 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...
Answers.UserDisplayName: dezbat
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-11T23:27:32.160Z
Answers.Text: I would love to know if this approach could work for related entries though :)
Answers.UserDisplayName: dezbat
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-11T23:33:10.033Z
Answers.Text: @geshud — I did try adding a subfolder 'entryTypes' for 'clients' and 'partners', then calling
  `{% include "entryTypes/" ~ entry.relatedTo('clients','partners') %}`
  — which did **not** work!
Answers.UserDisplayName: dezbat
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-11T07:26:01.397Z
Answers.LastActivityDate: 2024-12-11T09:11:28.163Z
Answers.LastEditDate: 2024-12-11T09:11:28.163Z
Answers.UserDisplayName: geschud
Body: <p>I have a page that lists a bunch of project data if it is present.</p>
  <p>At the moment the twig structure is doing something like:</p>
  <pre><code>{% 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 %}
  </code></pre>
  <p>etc... theres six or seven of these possible entry types I'm looking at.</p>
  <p>I am wondering if I can use a 'switch' setup like this:</p>
  <pre><code>{% switch entry.type %}
  
     {% case &quot;typeA&quot; %}
  
     {% case &quot;typeB&quot; %}
  
  
  {% endswitch %}
  </code></pre>
  <p>Following the <a href="https://craftcms.stackexchange.com/a/42141/16859">James Smith's suggestion</a>, I tried this to see if I could winkle out an associated 'client' — which definitely exists  — unfortunately this code below does not work either...</p>
  <pre><code>{% switch entry.type.handle %}
  
     {% case 'clients' %}
  
          {% for data in entry.clients.all() %}
                      
              {{ data.title }}
                              
          {% endfor %}
  
  
  
  {% endswitch %}
  </code></pre>
  <p>thanks for looking!</p>
  
Tags: <templating><craft5><switch>
Title: Can I 'switch' entry types rather than using if statements?
ThreadText: # Can I 'switch' entry types rather than using if statements?
  
  (Asked by: username_0 on 2024-12-10)
  
  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 six or seven of these possible entry types I'm looking at.
  
  I am wondering if I can use a 'switch' setup like this:
  
  ```
  {% switch entry.type %}
  
     {% case "typeA" %}
  
     {% case "typeB" %}
  
  {% endswitch %}
  
  ```
  
  Following the [username_1's suggestion](https://craftcms.stackexchange.com/a/42141/16859), 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!
  
  **Tags:** templating, craft5, switch
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_0 on 2024-12-11. Score: 0 | Accepted answer
  
  I figured this out — with a steer from the help here and also [Ryan Irelan's article](https://craftquest.io/articles/switch-statements-in-twig)
  
  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 data %}
                  C
              {% endfor %}
  
          {% case 'partners' %}
  
              {% for partner in data %}
                  P
              {% endfor %}
  
      {% endswitch %}
  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_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`  
  > **username_0**: Thanks @username_1! Have edited this reply accordingly...  
  
  ---
  
  ### Answer by username_1 on 2024-12-10. Score: 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>
  
  > **Comments:**
  > **username_0**: Thanks for looking @username_1 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  
  > **username_1**: 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.  
  > **username_0**: thanks again — unfortunately that doesn't seem to work, see above, I have emended the question to clarify what has already been attempted...  
  
  ---
  
  ### Answer by username_2 on 2024-12-14. Score: 1
  
  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!
  
  
  ---
  
  ### Answer by username_2 on 2024-12-11. Score: 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:
  
  ```none
  {# templates/entrytypes/partnersOverview.twig #}
  
  {% for data in entry.partners.all() %}
      {{ data.title }}
  {% endfor %}
  
  ```
  
  ```none
  {# templates/entrytypes/awardsOverview.twig #}
  
  {% for data in entry.awards.all() %}
    {{ data.title }}
  {% endfor %}
  
  ```
  
  And the "main" template
  
  ```none
  {# templates/main.twig #}
  
  {% include "entryTypes/" ~ entry.type #}
  
  ```
  
  > **Comments:**
  > **username_0**: 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...  
  > **username_0**: I would love to know if this approach could work for related entries though :)  
  > **username_0**: @geshud — I did try adding a subfolder 'entryTypes' for 'clients' and 'partners', then calling `{% include "entryTypes/" ~ entry.relatedTo('clients','partners') %}` — which did **not** work!  
  
  ---
  
  