1

Is it possible to do the following?

{% if entry.type == 'overview' %}

    {{ entry.title }}

{% elseif entry.type == ['availability', 'specifications', 'sitemap', 'location'] %}

    {{ entry.title }}

{% else %}

    {{ entry.title }}

{% endif %}

At the minute it is not returning anything inside the elseif, can anyone spot anything wrong with the logic or is there an alternative method?

2 Answers 2

3

You were so close! Instead of asking whether entry.type is equal to the array (which it isn't), you need to find out whether the value of entry.type matches one of the array items.

Here is an updated snippet that should work for you:

{% if entry.type == 'overview' %}
    {{ entry.title }}
{% elseif entry.type.handle in ['availability', 'specifications', 'sitemap', 'location'] %}
    {{ entry.title }}
{% else %}
    {{ entry.title }}
{% endif %}

EDIT:

It's important to recognize that entry.type is actually an object. When checking a value using in, the object's __toString method will not be invoked. Therefore you'll need explicitly specify entry.type.handle when testing whether the type is in an array.

0
2

Maybe off topic, but if you are looking to have a lot of logic based around entry types, maybe its easier to have one template per entry type?

If you set your section to use the template "sectionName/_entry", that entry can contain the code:

{% include['sectionName/' ~ entry.type, 'sectionName/default'] %}

This way you can have a template per entry type, with a fallback to default.twig inside the "sectionName" folder (inside your template folder).

Now you could have a overview.twig and sitemap.twig etc.

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.