gruntjs/grunt-contrib-handlebars

Handlebars Namespace config

Opened this issue · 3 comments

I really appreciate the namespace config option, so that I can put my templates in (e.g.) MyApp.Templates. However, I've also got my Handlebars namespaced as MyApp.Handlebars. Is there a way to make the generated JS templates obey that namespace? As of now, they look something like this (for a simple <h1>Hello, World.</h1> template):

this["MyApp"] = this["MyApp"] || {};
this["MyApp"]["Templates"] = this["MyApp"]["Templates"] || {};

this["MyApp"]["Templates"]["consumer/sweeps/test.hbs"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



  return "<h1>Hello, World.</h1>\n\n";
  });

Note the explicit call to Handlebars. Can I change that to (e.g.) MyApp.Handlebars so it looks something like this?

this["MyApp"] = this["MyApp"] || {};
this["MyApp"]["Templates"] = this["MyApp"]["Templates"] || {};

this["MyApp"]["Templates"]["consumer/sweeps/test.hbs"] = MyApp.Handlebars.template(function (MyApp.Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, MyApp.Handlebars.helpers); data = data || {};



  return "<h1>Hello, World.</h1>\n\n";
  });

I would also like to see this config option to avoid namespace conflicts. Trying to wrap my brain around Grunt and RequireJS, so much more of a headache it seems than Brunch!

Anyway, I'm stuck trying to move a project from compile-every-time Handlebars templates within script blocks (loaded via jQuery). If I leave the compiled output as-is, Handlebars is undefined within the AMD wrapper and execution fails at this point.

When I set wrapped to false in my Gruntfile options for grunt-contrib-handlebars, I can render templates if I call Handlebars.template() upon rendering -- but that is a bit cumbersome. I can also get the templates to work with this wrapping on by removing/renaming the first Handlebars arg from the AMD wrapper in the compiled templates file. The second definition of Handlebars within the inner template function then points to the correct requirejs.config path for the module (which is resolved as a bower component for the project). Unfortunately, any changes to the compiled output is obviously obliterated upon recompilation.

lazd commented

See my comment here, #96 (comment), it would solve this issue.

+1