Ghost blog not setting the right template for collection

My routes.yaml collections

collections:
  /blog/:
    permalink: /posts/{slug}/
    template: post
    data: tag.article
    filter: primary_tag:article+tag:-course
  /cursos/:
    permalink: /cursos/{slug}/
    template: course
    data: tag.course
    filter: primary_tag:course+tag:-article

The problem is: when accessing localhost:2368/cursos/{slug}/ the rendered page is based on post template and not course template.

Can someone help me?

Node: v10.13.0
Ghost-CLI version: 1.11.0
Ghost version: 2.30.2
Debian

The template file referenced in your routes.yaml file related to the “list view” of the content in question, but all individual posts are still rendered through the same post.hbs template.

If you want to have a different layout/template depending on collection, you can use the {{#has}} helper to load different code based on your filter properties. Eg:

{{#post}}
  {{#has tag="course"}}
     {{> "course"}}
  {{else}}
    {{> "article"}}
  {{/has}}
{{/post}}

Which would then load templates from partials/course.hbs and partials/article.hbs respectively

You shouldn’t need the inverse filter, btw. Each post can only ever have a single primary tag, so filtering your collections on primary tag alone should be enough :slight_smile:

collections:
 /blog/:
   permalink: /posts/{slug}/
   template: index
   data: tag.article
   filter: primary_tag:article
 /cursos/:
   permalink: /cursos/{slug}/
   template: courses
   data: tag.course
   filter: primary_tag:course

So what you would end up with here is:

  • index.hbs (list of blog posts)
    • partials/article.hbs (single blog post)
  • courses.hbs (list of courses)
    • partials/course.hbs (single course)

and the only thing you need to do to drive this functionality is to make sure the first tag on each post is either article or course

1 Like

Thank you very much for this answer, John.
It is working perfectly now!

Hello @John sorry to bother you.

I have the same issue, i try to apply your suggestions but it doesn’t work. Im surely doing something wrong

This is my routes.yml:

routes:
  /tos/:
    data: page.tos
    template: tos
  /privacy/:
    data: page.privacy
    template: privacy
  /about/:
    data: page.about
    template: about
  /influence/:
    template: case-studies
    controller: channel
    filter: tag:casestudies

collections:
  /case-study/:
    permalink: /case-study/{slug}/
    filter: tag:casestudies
    data: tag.casestudies
    template: case-post
  /:
    permalink: /{slug}/
    template: index

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

This is my posts.hbs

{{#post}}

{{#has tag="casestudies"}}

{{> case_study}}

{{/has}}

{{/post}}

The partial is not being displayed, but if I take it of the {{#has}} it works fine.

Just in case, this is my index.hbs from where you are redirected to the post itself:

 {{#get "posts" include="tags" filter="tag:casestudies"}}
          {{#foreach posts}}
           ....some html
                <a href="{{url}}" style="font-size: 12px;">Read more</a>
          ...some more html
          {{/foreach}}
 {{/get}}

Any suggestions?
Thank you!

@tomasyaya what does your partials/case_study.hbs file look like?

Do you have a {{#post}}{{/post}} in it? If so you’ll need to remove it because you’re already in the {{#post}} context when rendering the partial.

@Kevin first of all, thank you for your quick response.

I don’t have another context inside the partial, here is how it looks (partials/case_study.hbs):

<div>
    <section class="hero is-small">
        <div class="overlay policy">
            <div class="hero-body">
                <div class="container center">
                    <h1 class="title">Case Studies</h1>
                    <h2 class="subtitle">We work with the best influencers <br /> across the largest platforms.</h2>
                </div>
            </div>
    </section>
    <article class="case-study-article">
        <div class="case-study-img-container">
            <img src="{{feature_image}}" alt="">
        </div>
        <header class="case-study-title">
            <h2>{{title}}</h2>
        </header>
        <section>
            {{content}}
        </section>
    </article>
</div> 

Inside the post.hbs this works:

{{#post}}

{{> case_study}}

{{#has tag="casestudies"}}

{{/has}}

{{/post}}

But this don’t:

{{#post}}

{{#has tag="casestudies"}}
  {{> case_study}}
{{/has}}

{{/post}}

This other example returns “no content”

{{#post}}

{{#has tag="casestudies"}}

{{> case_study}}

{{else}}

<h2>no content</h2>

{{/has}}

{{/post}}

Let me know if I can give you any more clarifications or examples.

I appreciate it

@Kevin @DavidDarnes sorry to bother you guys.

But could you give me a hand with this? Or refer me somewhere, I cant seem to use different layouts in the posts till page till I figure this out.

Thank you !

Hey! There’s a really good example here on our docs, which appears to wrap the partial name in double quotes. Not sure if that would make a difference?

@DavidDarnes thanks for the recommendation I have a look at it.

I worked around it by looping all the tags and use an if in the primary_tag.

Don’t understand why the has helper is not working for me.

Thanks again

1 Like