<p>It's a little unclear what you need to achieve here –</p>

<p>If your entries' <a href="https://craftcms.com/features/entry-types" rel="nofollow noreferrer">entry type</a> handles actually <em>are</em> <code>'contact'</code> and <code>'location'</code>, the above code should work; which means that you've got an error somewhere that isn't really possible to figure out based on your description – e.g. the entry type handle is actually <code>contacts</code>, and not <code>contact</code>, or something like that.</p>

<p>One thing you might try, is to simply print the <code>entry.type</code> variable in a template, to see if it contains what you expect it to contain:</p>

<pre><code>{{ entry.type }}
</code></pre>

<p>On the other hand, if your issue is simply that some entries have a <code>contact</code> field populated and others have a <code>location</code> field populated, you can't really use a <code>{% switch %}</code> statement in any sensible manner for that – though a plain old <code>{% if %}</code> would do it:</p>

<pre><code>{% if entry.contact %}
    {{ entry.contact }}
{% elseif entry.location %}
    {{ entry.location }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2017-09-12T21:28:09.997Z
Thanks, Mat! I was basically trying to do what you described, which is to use a switch statement rather than a bunch of IF statements. Good to know that wouldn't work. I was figuring using a switch was perhaps a better, less taxing way to set up all my fields, check to see if they're populated, and then have them print if the field is populated rathering than using a bunch of IFs. Thanks again!
user3092
CC BY-SA 3.0
2017-09-12T21:30:10.330Z
No worries. In terms of _less taxin_, I really doubt you'll find any difference in performance in switch statements vs. regular if conditionals, one way or the other – switch statements can offer a more clear/readable syntax in some situations (and I don't think yours is a plausible use case for them), but that's about it :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2017-09-08T23:48:54.187Z
2017-09-08T23:48:54.187Z
Mats Mikkel Rummelhoff
<p>I'm trying to create a switch statement for a single entry event template that has various entry fields associated with it (description, contact, location, etc.) from an Events Channel I have. Rather than using a bunch of IF's to display only those fields that have been populated, I'm trying to use a switch statement instead, but I can't get this to work when I based the switch on the 'entry.type." I'm trying to something like this, but it's not working. Any help would be appreciated!</p>

<pre><code>{% switch entry.type %}
{% case 'contact' %}
    {{ entry.contact }}
{% case 'location' %}
    {{ entry.location }}
{% endswitch %}
</code></pre>

<switch>
Creating a switch based on entry handle
# Creating a switch based on entry handle

(Asked by: username_0 on 2017-09-08)

I'm trying to create a switch statement for a single entry event template that has various entry fields associated with it (description, contact, location, etc.) from an Events Channel I have. Rather than using a bunch of IF's to display only those fields that have been populated, I'm trying to use a switch statement instead, but I can't get this to work when I based the switch on the 'entry.type." I'm trying to something like this, but it's not working. Any help would be appreciated!
{% switch entry.type %}
{% case 'contact' %}
    {{ entry.contact }}
{% case 'location' %}
    {{ entry.location }}
{% endswitch %}

**Tags:** switch

---

## 1 answers

### Answer by username_1 on 2017-09-08. Score: 1

It's a little unclear what you need to achieve here –
If your entries' entry type handles actually are 'contact' and 'location', the above code should work; which means that you've got an error somewhere that isn't really possible to figure out based on your description – e.g. the entry type handle is actually contacts, and not contact, or something like that.
One thing you might try, is to simply print the entry.type variable in a template, to see if it contains what you expect it to contain:
{{ entry.type }}

On the other hand, if your issue is simply that some entries have a contact field populated and others have a location field populated, you can't really use a {% switch %} statement in any sensible manner for that – though a plain old {% if %} would do it:
{% if entry.contact %}
    {{ entry.contact }}
{% elseif entry.location %}
    {{ entry.location }}
{% endif %}

> **Comments:**
> **username_0**: Thanks, Mat! I was basically trying to do what you described, which is to use a switch statement rather than a bunch of IF statements. Good to know that wouldn't work. I was figuring using a switch was perhaps a better, less taxing way to set up all my fields, check to see if they're populated, and then have them print if the field is populated rathering than using a bunch of IFs. Thanks again!  
> **username_1**: No worries. In terms of _less taxin_, I really doubt you'll find any difference in performance in switch statements vs. regular if conditionals, one way or the other – switch statements can offer a more clear/readable syntax in some situations (and I don't think yours is a plausible use case for them), but that's about it :)  

---

