Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log a message to the console in Handlebars?

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?

like image 891
HaoQi Li Avatar asked Jun 10 '13 15:06

HaoQi Li


People also ask

Can you console log in handlebars?

You need to modify the Handlebars object before logging it to the console. Stringify it and use triple curly braces.

How do I console log?

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.

How do you comment out handlebars?

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.

What is console log in code?

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.


2 Answers

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);
  });
like image 59
selvagsz Avatar answered Oct 24 '22 23:10

selvagsz


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.

like image 20
cbnz Avatar answered Oct 25 '22 01:10

cbnz