提前感谢您的时间和帮助。
我正在尝试使用grunt-contrib-handlebars预编译把手(.hbs)模板
当我运行运行任务时,我最终得到了这个:
this["JST"] = this["JST"] || {};
this["JST"]["app/templates/err.hbs"] = 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;
});
但是,如果我从终端运行npm handlebars模块,我会得到:
(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;
});
})();
第二个编译模板(从终端运行)在我的应用程序中正常工作 - 但是grunt创建的模板没有。有人可能会指出我正确的方向,我可能在这里做错了吗?
我的gruntfile看起来像这样:
handlebars:
options:
wrapped: true
compile:
files: 'www/js/templates.js': ['app/templates/*.hbs']
再次感谢您的帮助!
答案 0 :(得分:1)
原来这是使用不同版本的Handlebars引起的问题,其中grunt-contrib-handlebars版本与全局安装的把手npm模块不同。
答案 1 :(得分:0)
你也可以使用一个函数作为&#34; processName&#34;并指定一个&#34;命名空间&#34;在Gruntfile中,例如,如果你在一个名为article_snippet.handlebars
的文件夹中只有一个文件handlebars
就可以完成这项工作:
handlebars: {
compile: {
options: {
namespace: 'Handlebars.templates',
processName: function(filename) {
var name = filenaname.split('/')[1].split('.');
return name[0];
},
wrapped: true,
commonjs: null
},
files: {
"js/articles/templates.js": "handlebars/article_snippet.handlebars",
}
}
},