根据这篇博客文章,我应该注册一个助手来更好地调试handlebars模板,但它不起作用:
< p >< code >ReferenceError: Handlebars未定义< /code >
那么,在Meteor/handlebars中如何使用< code >{{debug}}< /code >呢?
这是我在自己的项目中用于调试的帮助函数:
Template.registerHelper("debug", function(optionalValue) {
console.log("Current Context");
console.log("====================");
console.log(this);
if (optionalValue) {
console.log("Value");
console.log("====================");
console.log(optionalValue);
}
});
你可以使用{{debug}}
在模板中调用它,并显示当前所在的上下文。更多信息请参见http://docs.meteor.com/#/full/template_registerhelper。
Template.myTemplate.helpers({
helper: function () {
// some code here
console.log(arguments);
}
});
不需要直接调用Handlebars。
Handlebars.registerHelper('helper', function() {
// Do stuff
});
这个应该可以通过你的模板中的{{helper}}
来调用。
Template.registerHelper('helper', helperFunc);
使用Handlebars.regsterHelper('h',f);
的替代方案更好,因为如果您决定在将来的某个时候使用其他替代品(例如Spacebars,Meteor的真实名称)如Meteor Jade,那么您的应用程序不需要进行太多的重构。
这实际上是针对被接受的答案的评论。期待有一天能获得50点声望。
Template.myTemplate.helpers
。 - zVictor