patrickkettner / grunt-compile-handlebars

grunt plugin to compile static html from a handlebars plugin
MIT License
115 stars 29 forks source link

Helpers #23

Closed mamzellejuu closed 10 years ago

mamzellejuu commented 10 years ago

Hi,

do you have example how helpers need to be formated to be functionnal ?

patrickkettner commented 10 years ago

I don't have any examples handy - what trouble are you having?

On Fri, May 30, 2014 at 12:30 PM, mamzellejuu notifications@github.com wrote:

Hi,

do you have example how helpers need to be formated to be functionnal ?

Reply to this email directly or view it on GitHub: https://github.com/patrickkettner/grunt-compile-handlebars/issues/23

mamzellejuu commented 10 years ago

Find the solution, need to name the file the name of the helper name

ryan-ludwig commented 10 years ago

Hi there,

I'm also having troubles with helpers. Everything runs fine until I try to include them.

I have a very simple helper, howdy.js

Handlebars.registerHelper("howdy", function(){
  return "howdy";
});

My grunt file looks like

    'compile-handlebars': {
      dist: {
        template: '<%= config.src %>/templates/**/*.hbs',
        templateData: '<%= config.src %>/data/**/*.json',
        partials: '<%= config.src %>/partials/**/*.hbs',
        helpers: '<%= config.src %>/helpers/**/*.js',
        output: '<%= config.dist %>/*.html',
      },
    },

When I run Grunt, I get the error, Running "compile-handlebars:dist" (compile-handlebars) task Warning: Handlebars is not defined Use --force to continue."

Everything runs fine if I remove the helpers option in the grunt file, but then I can't use helpers. Any ideas?

patrickkettner commented 10 years ago

Hey @ryan-ludwig!

Seems like your handlebars helper isn't including handlebars.

By default, you only need to export a function matching the function signature of a handlebars helper.

that means rather than

Handlebars.registerHelper("howdy", function(){
  return "howdy";
});

you can have a file named howdy.js, that looks like

module.exports =  function() {
  return "howdy";
};

alternatively , requireing handlebars in your helper file should work,

var Handlebars = require('handlebars');

Handlebars.registerHelper("howdy", function(){
  return "howdy";
});

though I honestly haven't tried that previously, and can't check right now (on a mobile).

ryan-ludwig commented 10 years ago

Thanks a lot for the explanation @patrickkettner! Worked great.

patrickkettner commented 10 years ago

Its a pleasure! Happy to help

On Fri, Jul 25, 2014 at 9:47 PM, Ryan Ludwig notifications@github.com wrote:

Thanks a lot for the explanation @patrickkettner! Worked great.

Reply to this email directly or view it on GitHub: https://github.com/patrickkettner/grunt-compile-handlebars/issues/23#issuecomment-50219936