Search code examples
handlebars.jstemplate-engineassemblegrunt-assemble

Assemble: register handlebar helper function


I'm using assemble 0.4.17 which has bundled handlebar 1.3.0.
I'm trying to add a custom handlebar helper as documented here.

So I added this to my Gruntfile (at the bottom of the file, outside of module.exports = function(grunt) {)

Gruntfile.js

module.exports.asdf = function (str)  {  return 'asdf here!'; };

And added this to
index.hbs

{{#asdf}}
  bobo
{{/asdf}}

And I would suggest that asdf here! would show up in the generated html, but it does not, instead only a blank line is printed. I also tried the module.exports.register = function (Handlebars, options) method, but this didn't work as well. Do I need to add something else to add this handlebar helper?

I'm new to Assemble and grunt and handlebar, so I might just be missing the obvious


Solution

  • The helpers should be declared in another file and added to the helpers option in your assemble target:

    my-helper.js

    module.exports.asdf = function (str) { return 'asdf here!'; };
    

    Gruntfile.js

    grunt.initConfig({
      assemble: {
        options: {
          helpers: ['./my-helper.js']
        },
        someTarget: {
          ...
        }
      }
    });