Creating Templates for Each Individual Entry Type

If you have a section with multiple Entry Types and you want to give each one its own template, here’s a quick tutorial that shows how you can do it.

For the purposes of this tutorial, we’ll call our section “Media”, and give it a Template setting of “media/_entry”. Our section will have three Entry Types: “Audio”, “Photo”, and “Video”.

First you’ll need to create the actual templates for each Entry Type, as well as the main section template. We’ll put them in a hidden _types folder alongside media/_entry.html. Our complete craft/templates/media/ folder might end up looking something like this:

  • media/_entry.html
  • media/_types/audio.html
  • media/_types/photo.html
  • media/_types/video.html
  • media/index.html

Now that we’ve got our templates in place, with the template names matching the Entry Type handles, routing requests to the Entry Type-specific templates will be as simple as an {% include %} tag.

Here’s media/_entry.html:

{% include "media/_types/" ~ entry.type %}

If you have any entry types that don’t need their own special template, you can create a default template located at media/_types/default.html, and tell the {% include %} tag to use it as a fallback:

{% include ["media/_types/" ~ entry.type, "media/_types/default"] %}

That’s it! When a request comes in for one of the Media section’s entries, it will get routed to media/_entry.html, which in turn will include the appropriate Entry Type template (or possibly the fallback template). The ‘entry’ variable that automatically gets set in media/_entry.html will also be available to the included templates.