zVictor
zVictor

Reputation: 3717

How to debug template in Meteor/handlebars?

According to this blog post, I should register a helper to better debug handlebars templates, but is not working:

ReferenceError: Handlebars is not defined

So, how can I {{debug}} in Meteor/handlebars?

Upvotes: 6

Views: 5499

Answers (4)

Erlend V
Erlend V

Reputation: 518

This is the helper function I use for debugging in my own projects:

Template.registerHelper("debug", function(optionalValue) { 
  console.log("Current Context");
  console.log("====================");
  console.log(this);

  if (optionalValue) {
    console.log("Value"); 
    console.log("===================="); 
    console.log(optionalValue); 
  } 
});

You can then call it in your templates with {{debug}} and it displays the context you are currently in. Read more at http://docs.meteor.com/#/full/template_registerhelper.

Upvotes: 15

Nicolai S
Nicolai S

Reputation: 134

For the sake of completeness: you can also use

Template.registerHelper('helper', helperFunc);

instead of Handlebars.regsterHelper('h',f);

A small reason this is better is that then your app won't need that much refactoring if you decide somewhere down the road that you want to use something else instead of Handlebars(i.e. Spacebars, the real name of meteors adaption) like jade for meteor.

This is really a comment to the accepted answer. Looking forward to one day hit 50 rep.

Upvotes: 2

Joscha
Joscha

Reputation: 4693

In Meteor 0.4.0 you register handlers like this:

Template.myTemplate.helpers({
  helper: function () {
    // some code here
    console.log(arguments);
  }
});

There is no need to call Handlebars directly.

Upvotes: 6

Spiralis
Spiralis

Reputation: 3352

Make sure that you register your helper in client (or shared) meteor code.

Handlebars.registerHelper('helper', function() {
  // Do stuff
});

This should be callable via {{helper}} in your templates.

Upvotes: 4

Related Questions