I'm working with Ember.js and wondering if there is a built-in function in Handlebars that allows me to print to the console like {{ log "my message" }}
similar to what we can currently do with logging objects like: {{ log this }}
?
Or do I have to define my helper function every time?
But that is even not working for me (click for jsbin):
I have in the HTML Handlebars:
{{ debug "this is my string" }}
Then in app.js I have:
Ember.Handlebars.helper('debug', function(the_string){
console.log(the_string);
});
But app.js is not receiving the_string
, so the_string
is undefined there, what's going on?
You need to modify the Handlebars object before logging it to the console. Stringify it and use triple curly braces.
Steps to Open the Console Log in Google Chrome By default, the Inspect will open the "Elements" tab in the Developer Tools. Click on the "Console" tab which is to the right of "Elements". Now you can see the Console and any output that has been written to the Console log.
Template comments If you'd like the comments to show up just use HTML comments, and they will be output. Any comments that must contain }} or other handlebars tokens should use the {{!-- --}} syntax.
console. log specifically is a method for developers to write code to inconspicuously inform the developers what the code is doing. It can be used to alert you that there's an issue, but shouldn't take the place of an interactive debugger when it comes time to debug the code.
I'm not sure why Ember.Handlebars.helper doesn't work... As of now you can try
Ember.Handlebars.registerHelper('debug', function(the_string){
Ember.Logger.log(the_string);
// or simply
console.log(the_string);
});
Just posting a new answer for people who find this in future. There is an easier way now.
Your {{debug}}
helper is effectively built-in with the native {{log}}
helper. You can also add a breakpoint with the {{debugger}}
helper.
See the guides for more info.
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