How to quickly output JSON in handlebars
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
Global helper in express
Here is an easy way to add the above helper into your express project using express-handlebars
.
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}}}