Check current open post in Handlebars

Hello everybody!
I am creating a super minimalist theme and one of the main distinctive features is that the entire list of posts is found on every page, wherever you go, hidden in <details>. This means that if you open a post, you can still find a link to it in the bottom of the page, amongst other links to all posts. So I want to make this one particular link stand out.

I have a {{#foreach}} to iterate over all posts and then display them like this (with other formatting features, of course)

<a href="{{url absolute="true"}}">
  <p>{{title}}</p>
  <hr>
  <time>{{date format="D"}}</time>
</a>

I want to insert a current class or something in <a> if the link goes to the post that is currently open, so that it doesn’t get lost in the long list and to aid the navigation.

I tried some ideas with {{#match}} and {{has}}, but nothing worked :frowning:

Do you know how I can implement that? Am I missing something basic?

Thanks a lot! Have a nice day, people :slight_smile:

Hey @niklevenets,

You’d want to compare the id of post in a loop and the parent post (currently open post).

{{#match id ../../id}}current{{/match}}

1 Like

Doesn’t work. In fact, ../../id doesn’t give any output if I check it outside of {{#match}}.

Don’t know if this can help, but the current post opens at mysite.com/post_slug

make sure you’re using it inside the {{#post}}{{/post}} block inside post.hbs

2 Likes

Oh, thank you, sir! I really missed this one…

1 Like