Using Express, I find myself in need of having some bootstrapped data for the front end when the backend has done some processing. Here is a really easy way to output a json object into your handlebars template.

The helper function

var Handlebars.registerHelper('toJSON', function(object){
	return new Handlebars.SafeString(JSON.stringify(object));

Global helper in express

Here is an easy way to add the above helper into your express project using express-handlebars.

app.engine('handlebars', handlebars({
  defaultLayout: 'main', 
  helpers: {
    toJSON : function(object) {
      return JSON.stringify(object);

Handlebars template

If you use the helper function defined with Handlebars.SafeString add this in your template:

{{toJSON someJSON}}

And if you don’t use the Handlebars.SafeString method you will need to prevent the auto-escaping that

{{ }}

will do.

{{{toJSON someJSON}}}