Assigning a template to a collection in routes

Newbie here, so apologies if this is common knowledge…

Trying to create a collection with a unique post template. I have the collection working, but it isn’t using the template I’m expecting, so clearly I’m doing something wrong.

collections:
  /status/:
    permalink: /status/{slug}/
    filter: tag:status
    template: status

I’m expecting it to use status.hbs from within the root theme directory, but it just uses the default.

I know I can do custom-status.hbs and select the proper template from the editor, but since all posts in this collection should use this custom template, it makes more sense to do it within routes.

What am I doing wrong?

1 Like

Hey there!

Looks like you’ve got the majority of it right, but I think filter: tag:status is meant to be filter: primary_tag:status? There’s some documentation on the Ghost docs just here Ghost Themes - Dynamic URLs & Routing

Thanks for getting back. Actually the filtering seems to be working just fine. It’s the linking up with the status.hbs which doesn’t seem to be working. Any tips there?

Ah ok, sorry. Do you have a copy of your entire routes.yaml file? Maybe there’s some sort of conflict?

Here’s the full thing. It wouldn’t be related to the status route I have defined for the index view at the top, would it?

  /status/:
    controller: channel
    filter: tag:status
    template: index
  /reviews/:
    controller: channel
    filter: tag:review
    template: index

collections:
  /:
    permalink: /{slug}/
    filter: tag:blog
    template: index
  /status/:
    permalink: /status/{slug}/
    filter: tag:status
    template: status
  /reviews/:
    permalink: /review/{slug}/
    filter: tag:review
    template: index

taxonomies:
  tag: /tag/{slug}/
  author: /author/{slug}/

It could be that section at the top yea, have you tried removing it? Looks like it’s defining the same things really

I’m having same issue. I was expecting the posts inside the collection manual use manual.hbs. But, just the the /manual/ are using this template.

@DavidDarnes can you see some error on routes.yaml?
The posts with /manual/slug, are getting the posts.hbs template instead.

I already restarted ghost too. Here my routes and template manual.hbs

routes:
  /estrategia/:
    controller: channel
    filter: tag:[manual,estrategia]

collections:
  /:
    permalink: /{slug}/
    template: index
    filter: tag:-manual
  /manual/:
    permalink: /manual/{slug}/
    template: manual
    filter: primary_tag:manual
    order: featured desc, published_at desc

taxonomies:
  tag: /tag/{slug}/
  author: /author/{slug}/

Manual.hbs


{{!< default}}
{{!--manual.hbs--}}

{{#post}}

<main class="eis-manual-main">

  <aside class="eis-manual-sidebar">

    <h2><a href="#">Eis o Manual</a></h2>

    <nav>
      {{#get "posts" filter="tag:manual" as |posts|}}
      {{#foreach posts}}
      <ul>
        <li><a href="{{url}}">{{title}}</a></li>    
      </ul>
      {{/foreach}}
      {{/get}}
    </nav>

  </aside>

  <section class="eis-manual-content">

    <h1 class="eis-article-title">{{title}}</h1>
    <h2 class="eis-article-excerpt">{{excerpt}}</h2>
    <div class="eis-article-content">
    {{content}}
    </div>
  </section>

</main>

{{/post}}

I was doing some research, it seems this post have same issue.

Posts at /manual/*slug-post* uses the template defined in routes.yml automatically, right?

The collections template key is for setting the template of that listings page, not the individual posts. If you want to change the single post views design you’ll need to do that in the post template code I think

1 Like