By
on Ghost, HandleBars

Hacking and adding custom helpers to Ghost

Warning: The code I'm about to mention below isn't a good practise. As far as I am aware, Ghost does not currently support adding custom HandleBar helpers but plan on doing so soon.

I wanted to change the behavior of my blog so as to display the blog cover only on the home page and not on pages where pagination is being used(/page/2/). After a quick look at the helpers present on Ghost's documentation I couldn't find a suitable solution.

I started to read up on the code of Ghost. From the looks of it there are plans to add support for apps however that functionality does not exist yet. Here is the quick and dirty solution I came up with.

I added the following code in the core/server/helpers/index.js file

coreHelpers.ifOnHomePage = function (options) {  
  if ((config.paths.urlFor(this, false) === "/")) {
    return options.fn(this);
  }
  return options.inverse(this);
}

followed by registering it at the end of the same file

registerThemeHelper('ifOnHomePage', coreHelpers.ifOnHomePage);  

After adding the code in, the handlebars helper can now be used.

  {{#ifOnHomePage}}
    <h1>I work!</h1>
  {{/ifOnHomePage}}
comments powered by Disqus