【发布时间】:2015-08-17 11:23:52
【问题描述】:
根据这个blog post,我应该注册一个帮助程序来更好地调试车把模板,但不起作用:
ReferenceError: Handlebars is not defined
那么,我如何在 Meteor/handlebars 中 {{debug}}?
【问题讨论】:
标签: meteor handlebars.js
根据这个blog post,我应该注册一个帮助程序来更好地调试车把模板,但不起作用:
ReferenceError: Handlebars is not defined
那么,我如何在 Meteor/handlebars 中 {{debug}}?
【问题讨论】:
标签: meteor handlebars.js
这是我在自己的项目中用于调试的辅助函数:
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。
在 Meteor 0.4.0 中,您可以像这样注册处理程序:
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);
这样做更好的一个小原因是,如果您决定在某个地方使用其他东西而不是 Handlebars(即 Spacebars,流星适配的真实名称),那么您的应用程序将不需要那么多重构) 喜欢jade for meteor。
这确实是对accepted answer 的评论。期待有一天能达到 50 次。
【讨论】: