How to Create Custom Handlebar Helpers in Ghost?

Hi Ghost community! :wave:

Have you ever felt limited by the default Handlebars functionality in Ghost? I’ve got something for you! I’ve written a detailed article that explains how to create custom Handlebars helpers and integrate them into Ghost using your own middleware (reverse proxy) without modifying Ghost core.

In the article, you’ll find:

  • Why middleware is a great approach for this task?
  • Step-by-step examples for building custom helpers.
  • How to handle security and avoid XSS vulnerabilities.
  • Tips for creating asynchronous helpers using handlebars-async-helpers.
  • How to interact with Ghost database using custom helper, for example: to get current post object.
  • Performance optimization ideas and architectural implementation using Docker.

The article is packed with practical code examples, like decorating responses with userResDecorator, and explains how to utilize libraries such as express-http-proxy, handlebars, or cheerio.

I’d love to hear your feedback. If you’ve had your own experience with extending Ghost’s capabilities, feel free to share in the comments.

Thank you for your attention, and I hope the article proves helpful!

1 Like

Interesting approach. You may want to clarify in your article that method 2 requires database access for the examples shown, which means it won’t work with many managed “hosting providers”, including ghost pro.

For anyone trying it with ghost pro (without database access), you’ll want to activate the domain in ghost and then turn on the reverse proxy.

Be wary of issues with ssl certificate renewal.

Thank you for your comment, Cathy!

You’re absolutely right — unfortunately, this method won’t work for managed hosting environments. It all depends on the hosting provider. I’ll add a note about this to the article shortly. Regarding SSL certificates, you’re spot on —thankfully, Nginx has tools like Certbot that make certificate renewal much easier.

As for the middleware, I didn’t limit its use (in my case) only to custom helpers but also explored creating custom “cards” or custom “post fields” in the Ghost editor (Koenig). This is achievable, but I’m still in the development phase. Here’s a quick summary of the process:

  • In the editor, I needed custom fields for a post.
  • I used an ‘html’ card that contains JSON with various custom fields for the post.
  • During the middleware stage, this ‘html’ card with JSON is parsed and utilized for various purposes.
  • Finally, the middleware removes the JSON from the proxy’s final response, making it invisible to the end user.

Earlier, I read about how to achieve similar things, including insights from your article, which might explain the recent spike in views on it: Using the Ghost API to create snippets

2 Likes