gruntjs / grunt-contrib-handlebars

Precompile Handlebars templates to JST file.
http://gruntjs.com/
MIT License
282 stars 126 forks source link

Partials output before namespace declaration #122

Closed codegoat closed 9 years ago

codegoat commented 10 years ago

Since the 0.9 update, I have seen an issue where the partials are being output before the namespace, and because I am using the option partialsUseNameSpace: true, when the partials are output:

// partials
Handlebars.registerPartial("templates/_partial", this["JST"]["templates/_partial"] = ...

// namespace declaration
this["JST"] = this["JST"] || {};

// templates

I am not very familiar with this project so I may not be taking other things into consideration with my change, but see the attached PR, it tracks the nsDeclarations separately between the partials and templates.

codegoat commented 10 years ago

Here is an example of what I mean, I took the partials_use_namespace test and modified it to add another template file.

partials_use_namespace: {
    options: {
        partialsUseNamespace: true
    },
    files: {
        'tmp/partials_use_namespace.js': ['test/fixtures/has-spaces.hbs', 'test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
    }
},

Which generates this:


Handlebars.registerPartial("partial", this["JST"]["partial"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  return "<span>Canada</span>";
  },"useData":true}));

this["JST"] = this["JST"] || {};

this["JST"]["test/fixtures/has-spaces.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  return "\n\n    <div>\n        <span>this template has many spaces</span>\n    </div>\n";
  },"useData":true});

this["JST"]["test/fixtures/one.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  var stack1, helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression, buffer = "<p>Hello, my name is "
    + escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"name","hash":{},"data":data}) : helper)))
    + ". I live in ";
  stack1 = this.invokePartial(partials.partial, '', 'partial', depth0, undefined, helpers, partials, data);
  if (stack1 != null) { buffer += stack1; }
  return buffer + "</p>";
},"usePartial":true,"useData":true});

After the changes made in this PR, this is the result:


this["JST"] = this["JST"] || {};

Handlebars.registerPartial("partial", this["JST"]["partial"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  return "<span>Canada</span>";
  },"useData":true}));

this["JST"] = this["JST"] || {};

this["JST"]["test/fixtures/has-spaces.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  return "\n\n    <div>\n        <span>this template has many spaces</span>\n    </div>\n";
  },"useData":true});

this["JST"]["test/fixtures/one.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  var stack1, helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression, buffer = "<p>Hello, my name is "
    + escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"name","hash":{},"data":data}) : helper)))
    + ". I live in ";
  stack1 = this.invokePartial(partials.partial, '', 'partial', depth0, undefined, helpers, partials, data);
  if (stack1 != null) { buffer += stack1; }
  return buffer + "</p>";
},"usePartial":true,"useData":true});
lostthetrail commented 9 years ago

@codegoat Can you check 0.9.3 for this issue still? We merged in some of my changes and it resolved the issues for me. If so, please close this PR.