1

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?

4

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
            )
        )
    );
}
|improve this answer|||||
  • exactly what i was hoping for! Thanks Ben! – Keith Mancuso Jan 11 '15 at 19:40
0

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',
  '/_layouts/default.twig'
] %}

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

|improve this answer|||||
  • I think you misread the question Tim, it is in reference to element types and not entry types. – Ben Croker Jan 4 '15 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.