Use helper "has" outside post

Hi!

Is there a way to use has outside the post? Because the actual post.hbs structure is the following

{{#post}}
    ..........
{{/post}}

{{#has tag="work"}}
    {{#get "posts" limit="3" filter="tags:[{{post.tags}}]+id:-{{post.id}}" include="tags,authors" as |related|}}
        ....
    {{/get}}
{{/has}}

And the has is not working also doesn’t work if I move the get block into the post

Looks like you have a slight syntax issue, should look like this:

{{#post}}
    {{#has tag="work"}}
      {{#get "posts" limit="3" filter="tags:[{{post.tags}}]+id:-{{post.id}}" include="tags,authors" as |related|}}
        ....
    {{/get}}
  {{/has}}
{{/post}}

You can use the #has helper anywhere, but if you’re checking if a post has something then you’ll need to wrap it inside a {{#post}} {{/post}} so the #has helper has the right context.

Hope this helps :v:

Hi David,

Is there any reason is not working? Because I already did that, moving the get block within the post block.

Did you compare your code to mine? The sample code you posted had a missing “#” in {{#post}} :slightly_smiling_face:

Yes, I missed that in the original question but the # is in the post.hbs

Oh, in that case do you have a link to a public version of the code? Maybe on GitHub?

Well, I am using this theme https://github.com/eddiesigner/liebling/blob/master/post.hbs with some modifications in the style and routes for my website https://edgardorl.com

Ah cool! Do you mind sharing your routes file? It might be how the definition of the home page is setup

Not a problem, here you have it.

routes:
  /: me
  
collections:
  /blog/:
    permalink: /blog/{slug}/
    template: index
    filter: primary_tag:blog
  /projects/:
    permalink: /project/{slug}/
    template: portfolio
    filter: primary_tag:work
    data: tag.work

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

By the way I am doing this because I was trying to create another file only for the project pages like project.hbs but I couldn’t connect the posts with tag work with the project.hbs so that’s why I am doing this but temporary.

I see. Looks like your routes file is ok. So would I be right in saying you wanted to create a “related projects” section within a single project? Have you looked into custom templates? You can create new post templates called custom-project.hbs and then you’ll be able to select the template in the Ghost admin, there’s more info on it here:

Additionally, do you need the #has check? If you’re trying to get related posts then surely it’ll only get the posts that have any tags the same as the currently viewed one? For example a project post will only have project posts related to it :thinking:

1 Like

I was doing the wrong thing with custom templates, for a moment I thought custom was a wildcard but it’s not and now it’s working, I am using that custom-project… Ghost is more powerful than I thought.

Thank you David!

No problem! Also I’ve spoken with a colleague and there was a missing change to the code I supplied. Here’s what you should do when using post data inside of {{#post}}{{/post}}:

{{#post}}
  {{#has tag="work"}}
      {{#get "posts" limit="3" filter="tags:[{{tags}}]+id:-{{id}}" include="tags,authors" as |related|}}
        ....
    {{/get}}
  {{/has}}
{{/post}}

Sorry for the mistake and hope this helps!

Hi David,

I tried with your code but is not working and its because the {{tags}} within the post is not returning Objects but a links directly to be rendered.

Ah, that’ll be because {{tags}} renders the anchor link around the tag string. Check out our docs where it shows how you use tags in handlebars templates and how to loop through them to render out specific parts: