8

Each of my entry types have their own template.

The url is something like:

/foo/{entryTypeHandle}

And the route is:

'foo/(?P<routesEntryTypeHandle>[-\w]+)' => 'bar.html'

In the template I can get the section the entry type is in:

{% set section = craft.sections.getSectionById(5) %}

And I can get ALL the entry types:

{% set entryTypes = section.getEntryTypes() %}

And I can get all the entries of that entry type:

{% set entries = craft.entries.section(section).type(routesEntryTypeHandle) %}

But I can't seem to get the entry type's name.

In the Sections Service there's a method called getEntryTypesByHandle but that's not available in the model.

I could get the entry type name from the entries, but that relies on there actually being results, which isn't assured.

{{ entries.first.type.name }}

I must be missing an easier way. Anyone?

2 Answers 2

16

A simpler way to do this might be:

{{ entry.type.name }}
2
  • 1
    this is the correct answer.
    – ricricucit
    Commented Feb 22, 2018 at 18:38
  • There is also entry.type.handle which is a bit better for "code" checks. Commented Apr 29, 2022 at 2:38
10

Classic find the answer straight after posting.

The getEntryTypes method of the sections model allows you to choose how the array is indexed. This means you can do this:

{% set entryTypes = section.getEntryTypes('handle') %}

Which means our entry type model can be accessed using:

{% set entryType = entryTypes[routesEntryTypeHandle] %}

And the name:

{{ entryType.name }}
0

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.