gruntjs/grunt-contrib-handlebars

Compiled output different than terminal output

Closed this issue · 5 comments

I'm working on a project and I'm noticing a weird problem - may be my misunderstanding about how this module is supposed to work

When I run this grunt task I'm getting the following output

Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
  var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression;

  buffer += "<div>Error: ";
  if (stack1 = helpers.error) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.error; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</div>";
  return buffer;
  })

However, when I run the handlebars npm module from terminal I get a slightly different output:

(function() {
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['err.hbs'] = template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
  var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression;


  buffer += "<div>Error: ";
  if (stack1 = helpers.error) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.error; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</div>";
  return buffer;
  });
})();

As you can see the only difference is that the terminal output is wrapped in an anon function

(function() {
  var template = Handlebars ...
})();

However, it is making the difference between the template working and not working.

I'm using the following coffeescript:

data = error:thrownError
errorTemplate = Handlebars.templates['err.hbs']
temp = errorTemplate(data)

That code works with the terminal compiled template - but not with the grunt module compiled template.

Not sure why but thought I would post in case it is relevant.

Rich

Ping. I'm getting the same thing, would be very happy to know how to replicate the '-f' output.

Thanks

Did you have any luck resolving this? I'm having the same issue. Thanks!

This is because the grunt plugin does NOT run the command line handlebars tool. Nor does it use the command line tool's option handling, nor output production. Rather, it runs the compiler on each input file, wraps its own boilerplate around each compiled output, and concatenates them together. Possibly with some additional bracketing boilerplate, for amd, etc.

@billev2k is right; the default output from the CLI puts the templates in the Handlebars.templates array, but this task doesn't use the CLI command nor mimic it. The output from this task is more similar to passing the -s flag from the CLI, then working with the value of main in the produced JSON.

Right now there's no direct way to mimic the output of the -f flag. If you wanted to store it in the templates array, you could use the processContent option. Something like this might work if you're building a single template per target (note: I didn't test this, and it's probably not suited for production; it's just a quick example to get the idea across):

processContent: function(content, filepath) {
  return 'handlebars.templates[filepath] = ' + content;
}

For anyone else that finds this bug when dealing with the same issue (as I did), you may find that patrickkettner/grunt-compile-handlebars suits your needs better than this repo. It works well for me.