How to customise post.hbs based on tag

I’ve searched through a lot of threads on this topic and there appears to be no solution.

Say I have 3 tags: photos, videos and audio. They make their own collections page by using tag.hbs (i.e., photos.hbs, videos.hbs, and audio.hbs). All good:

Collections:
/photos/ ----> uses photos.hbs to render collection

Furthermore, one can customize things on post-by-post bases either by crreating custom-tag.hbs (i.e. custom-photos.hbs, custom-videos.hbs and custom-audio.hbs) and then assigning them in the UI when creating the post or some custom-{slug}.hbs methods.

Posts:
/photos/my-photo-1/ ----> uses custom-photos.hbs

However, I have to use the UI to assign custom-photos.hbs :frowning_face:. This is not convenient when you have hundreds of posts. It is great for customizing a handful of pages. I think this is an amazing feature already!

Posts by Tag?

There is currently no way to create post.hbs templates that automatically apply to a post with the associated tag.

How am I currently doing it?
I use {{is #tag}} selector in the post.hbs and I have 3 sections in post.hbs, each dedicated to rendering photos, videos and audio.

This one bit of how post.hbs is rendered on per tag basis would make Ghost literally general purpose for doing anything - not just blogging. It already sorta works with the hacky method of using {{is #tag}} in post.hbs.

I am sorry if I have missed some obvious way of doing this? Thank you!

Dynamic Routing should be able to handle this :thinking:

{{#has}} is the correct solution inside post.hbs and can be used however you want - it’s not a hack - that’s exactly what the helper is for.

Ghost is not general purpose, and is not intended to be general purpose. It is specifically intended for publishing and blogging. If you’re looking for a general purpose platform, there are lots of them out there.

3 Likes

@vikaspotluri123 How? Here is my routes.yml:

routes:
  /: home
collections:
  /photos/:
    permalink: /photos/{slug}/
    template: photos
    filter: primary_tag:photos
    data: tag.photos

  /videos/:
    permalink: /videos/{slug}/
    template: videos
    filter: primary_tag:videos
    data: tag.videos

  /audio/:
    permalink: /audio/{slug}/
    template: audio
    filter: primary_tag:audio
    data: tag.audio

@John Yea, I think I am using {{is #tag} or {{#has}}. But doesn’t that feel “hacky”, as in, it goes against the entire composability of the template system? Now my post.hbs looks like a giant switch statement with tons of HTML for all the 3 types of tagged posts want to render.

I very much despise anything that’s out there for a CMS platform. Once I’ve seen how Ghost works and running a blog, man…everything just pales in comparison. I respect the stance that it is a publishing platform, just that it is almost 90% a CMS :slightly_smiling_face:. Ghost is fricking amazing.

1 Like

You can clean up your giant post.hbs file with partials! We’re actually using them in our Casper theme for the subscribe form:

The partial code resides in partials/subscribe-form.hbs :slightly_smiling_face:

1 Like

Brilliant, thanks! @DavidDarnes

1 Like