4

I would like to create a route for the entry types of a specific section, pointing to a single template.

Is it possible to do this automatically? So far I haven't had luck, I'm thinking something like this:

  • route = /{ mysection.type }
  • template = type/index.twig

Or would I need to create a route for each entry type and all of them pointing to same template?

  • route 1 = /entryType1
  • route 2 = /entryType2
  • route 3 = /entryType3
  • shared template = type/index.twig

The reason I would like to do it this way, as opposed to how it has been shown before, here; is because I would like to keep all the URL's with a date format, i.e.: example.com/2014/07/11/slug so that I can then implement an archive template. But I still need a way to filter by entry types (a total of 12 types). So that at the end of the day I end up with URLs like these ones:

  • example.com/2014/01/01/example-entry - URL for Example Entry
  • example.com/2014/01/01/ - URL for listing entries from day
  • example.com/2014/01/ - URL for listing entries from month
  • example.com/2014/ - URL for listing entries from year
  • example.com/video/ - URL for Video types of entry
  • example.com/audio/ - URL for Audio types of entry

Any suggestions on how to solve this?

5

You can have entry type specific templates for your entry pages, see this docs article on how you can do this. And for your archive pages, just set a route for each of them and point it to the corresponding index/listings template.

So basically you end up with something like this for your entries:

  • media/_entry.html (example.com/2014/01/01/example-entry)
  • media/_types/audio.html (included in _entry.html if entry type is audio)
  • media/_types/video.html (included in _entry.html if entry type is video)
  • ...

and this for your archive pages:

  • media/index.html (example.com/2014/01/01/ etc.)
  • media/audio/index.html (example.com/audio/)
  • media/video/index.html (example.com/video/)
  • ...

If you don't want to set new routes for the entry type specific archive pages you can also put the templates in a folder whose index.html template file is called automatically if no route exists (eg. audio/index.html for example.com/audio/).

Additionally I'd make good use of include and extend in these templates to make this as DRY and easy to maintain as possible.

  • Ignore my comment. re-reading with update. – cballenar Jul 11 '14 at 16:53
  • Haha, sorry, I got pretty confused. I'm not sure I'm following your suggestion but I'm gonna try it and get back. Thanks! – cballenar Jul 11 '14 at 17:00
  • Ok, I see what you're saying now. Yes, that's actually the approach I'm taking at the moment. However since I have 12 entry types, this becomes cumbersome to set up and maintain if anything changes, especially when I need to get the type name and handle, and try to keep it consistent with URLs. See this other question I posted: craftcms.stackexchange.com/questions/772/… It's possible to do it this way, but it seems like there should be an easier way of handling it. Wouldn't you think? – cballenar Jul 11 '14 at 17:17
  • @cballenar I don't see the problem you have with this. In case you want to add entry types, all you have to do is to add a new archive template and a new entry template. If these templates are very similar you can always work with include / extend. – carlcs Jul 11 '14 at 17:22
  • 1
    @cballenar if you don't want to set new routes for the entry type specific archive pages you can also put the templates in a folder that is called automatically if no route exists (eg. audio/index.html for example.com/audio/). – carlcs Jul 11 '14 at 17:25

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.