如何在 Handlebars 模板中添加 console.log() JavaScript 逻辑? [英] How do I add console.log() JavaScript logic inside of a Handlebars template?
问题描述
我正在构建一个新的 Meteor 应用程序,但我无法弄清楚如何使用 Handlebars 添加 JavaScript 逻辑以在每次循环之前运行 console.log()
.在主干中,我会这样做,<% console.log(data);%>
来测试数据是否被传入.
我不知道如何使用 Meteor 和 Handlebars 做到这一点,而且我在他们的网站上找不到解决方案.
I'm in the process of building a new Meteor app and I can't figure out how to add JavaScript logic with Handlebars to run a console.log()
before my each loop. In backbone I would just do, <% console.log(data); %>
to test that the data was being passed in.
I'm not sure how to do this with Meteor and Handlebars and I couldn't find the solution on their site.
推荐答案
在您项目的客户端加载的 JavaScript 文件之一中创建一个 Handlebars 助手:
Create a Handlebars helper in one of the client-loaded JavaScript files in your project:
Template.registerHelper("log", function(something) {
console.log(something);
});
然后在您的模板中调用它:
And then call it in your template:
{{log someVariable}}
您可以使用简单的 {{log this}}
记录当前上下文.
You can log the current context with simply {{log this}}
.
(请注意,在 0.8 版本之前的 Meteor 中,或在 Meteor 应用程序之外的纯 Handlebars 中,将 Template.registerHelper
替换为 Handlebars.registerHelper
.)
(Note that in Meteor before version 0.8, or in pure Handlebars outside of a Meteor app, replace Template.registerHelper
with Handlebars.registerHelper
.)
这篇关于如何在 Handlebars 模板中添加 console.log() JavaScript 逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!