Accessing User ID and Post ID from a Post.hbs file in Theme

Hi there.

I am trying to implement a like button and view count functionality, but im finding it too difficult to implement this from core, thus I’m thinking if theres a way to implement it from the themes.

The idea is, that from the theme post.hbs file, i would make ajax calls to my custom service which contains the records of likes and views.

the issue is that i need to be able to identify:

  1. The current post.
  2. The user who is browsing the post, if they have signed in.

All i need is some kind of ID for both of these entities and i could implement this.

Please help me out here, and feel free to suggest any other way to approch this. Thanks a lot.

I think you could approach this in a couple of different ways:

  1. In a script tag in the theme, use Handlebars to output variables that are consumable by the API.
const postId = "{{id}}";
  1. Add a data attribute to an HTML tag, which you can then access via JS.
<article class="post" data-post-id="{{id}}">

For which properties are available, you can check out the docs: