我遇到了Handlebars編譯模板的問題。我覺得我完全錯過了一些重要的東西,但儘可能地嘗試,我似乎無法解決這個問題,或者在網上找到任何有關這種特殊情況出現的信息。Handlebars:TypeError:無法讀取undefined屬性'helperMissing'
我正在使用gulp-handlebars編譯使用gulp任務。吞氣任務是:
gulp.task('build-hbs', function(){
gulp.src('root/app/src/hbs/*.hbs')
.pipe(handlebars())
.on('error', notify.onError({
message: 'Error: <%= error.message %>'
}))
.pipe(declare({
namespace: 'Handlebars.templates',
noRedeclare: true // Avoid duplicate declarations
}))
.pipe(concat('templates.js'))
.pipe(gulp.dest('root/app/js/templates/'));
});
簡單的模板東西都工作正常,但是我現在試圖用一個簡單的輔助(注:與非編譯模板工作時,助手正常工作)。助手是:
Handlebars.registerHelper('gravatar', function(hash, size) {
var grav = '<img src="http://www.gravatar.com/avatar/' + hash + '.jpg?r=g&d=mm&s=' + size + '">';
return new Handlebars.SafeString(grav);
});
模板本身看起來是這樣的:
<div id="nick"><b>{{display}}</b></div>
<div id="image">{{gravatar hash}}</div>
編譯後,我得到:
this["Handlebars"] = this["Handlebars"] || {};
this["Handlebars"]["templates"] = this["Handlebars"]["templates"] || {};
this["Handlebars"]["templates"]["profile"] = {"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
var helper, alias1=helpers.helperMissing, alias2="function", alias3=this.escapeExpression;
return "<div id=\"nick\">\r\n <b>"
+ alias3(((helper = (helper = helpers.display || (depth0 != null ? depth0.display : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"display","hash":{},"data":data}) : helper)))
+ "</b>\r\n</div>\r\n<div id=\"image\">\r\n <img src=\"http://www.gravatar.com/avatar/"
+ alias3(((helper = (helper = helpers.hash || (depth0 != null ? depth0.hash : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"hash","hash":{},"data":data}) : helper)))
+ "?s=32\">\r\n</div>";
},"useData":true};
在我的JS代碼,我做這樣的事情:
$('#profile').html(Handlebars.templates.profile({
display:'user 1',
hash:'abdcef....'
}));
當我運行代碼I得到的錯誤:
TypeError: Cannot read property 'helperMissing' of undefined
裏面涉及到編譯模板代碼:
...
var helper, alias1=helpers.helperMissing, alias2="function", alias3=this.escapeExpression;
...
我似乎無法找到任何原因的代碼被添加,或者到helperMissing
功能的任何引用把手文件...
任何有關爲什麼這可能會發生的見解將非常歡迎!