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.
You need to modify the Handlebars object before logging it to the console. Stringify it and use triple curly braces.
Define the template that is packed with handlebars JavaScript syntax. Compile the template with handlebars JavaScript compile method. Provide the data context i.e. data from server-side in a form of JSON to map to the template. Insert or append the final HTML into your designated DOM location of the HTML page.
Handlebars. js is a Javascript library used to create reusable webpage templates. The templates are combination of HTML, text, and expressions. The expressions are included in the html document and surrounded by double curly braces.
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}}
You can log the current context with simply {{log this}}
.
(Note that in Meteor before version 0.8, or in pure Handlebars outside of a Meteor app, replace Template.registerHelper
with Handlebars.registerHelper
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With