Templates for posts in a collection

I created a collection in my routes.yaml file so I can have posts show up with the primary tag in the URL, like so:

routes.yml

collections:
  /favorites/:
    permalink: /favorites/{slug}/
    template: favorites
    filter: primary_tag:favorites

Results

  • /favorites uses the favorites.hbs template
  • /favorites/{slug} uses the post.hbs template

I can’t figure out how to get the individual posts to use a template other than post.hbs. Seems like tag-favorites.hbs should do the trick, but everything under /favorites/{slug} is still loading post.hbs (and yes, I restarted Ghost). Any tips on how to achieve this?

Hi @coolmoods,

It seems you are looking to create custom post templates.

After you created a custom template you can assign it to any post in the Admin:

Ah, OK. I was hoping I wouldn’t have to make the editors manually choose a template (more room for goofs), but it’s good to know it’s an option.

I found a viable workaround:

  1. Create your various post templates in the partials subdirectory.

  2. Edit your main post.hbs to include the correct template based on tag criteria:

{{!< default}}

{{#post}}
  {{#has tag="newsletter"}}
    {{> post-newsletter ..}}
  {{else has tag="podcast"}}
    {{> post-podcast ..}}
  {{else}}
    {{> post-generic ..}}
  {{/has}}
{{/post}}
2 Likes