简体   繁体   中英

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

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: {
      ...
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM