So I'm working on a prototyping plugin that has its own element type for pages. My hope was to have a single _page.html template and much like entries or categories be able to tell Craft to map the routes for all the elements URIs to that template?

But not having much luck figuring out how to do that?

I have URIs saving with my elements but how do I note which template to load?

I assume it has something to do with registering routes but I'm not exactly sure where to start?

2 Answers 2


You'll need to override the BaseElementType's routeRequestForMatchedElement method in your element type class as follows:

public function routeRequestForMatchedElement(BaseElementModel $element)
    return array(
        'action' => 'templates/render',
        'params' => array(
            'template' => '_page',
            'variables' => array(
                'page' => $element
  • exactly what i was hoping for! Thanks Ben! Jan 11, 2015 at 19:40

You could always route that section through a template that routes based on entry type. Something like this:

{% set section = entry.section.handle|default %}
{% set entryType = entry.type|default('default') %}
{% extends [
  '_sections/' ~ section ~ '/' ~ entryType ~ '.twig',
  '_sections/' ~ section ~ '/default.twig',
] %}

This way it will extends any template matching that type, or fall back to defaults.

  • I think you misread the question Tim, it is in reference to element types and not entry types.
    – Ben Croker
    Jan 4, 2015 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.