Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

node.js and Handlebars: HTML compiled is escaped

node.js and Handlebars: HTML compiled is escaped

Problem

Im using Handlebars in a node aplication, and I have trouble.

This is the template index.html

{{CONTENT}}

This is the code

var fs = require("fs");
var handlebars = require("handlebars");

var data = {
    CONTENT: "Hello world!"
};

var templateFile = fs.readFileSync('./index.html', 'utf8');
var template = handlebars.compile( templateFile );
var html = template(data);

The problem is that the tags are escaped to <B>

How can I avoid this?

Problem courtesy of: Enrique Moreno Tent

Solution

From handlebarsjs.com :

Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash".

{{title}}

{{{body}}}

with this context:

{
  title: "All about 

Tags", body: "

This is a post about <p> tags

" }

results in:

All About <p> Tags

This is a post about <p> tags

However from my point of view it may defeat the purpose of having a template separated than you're js file.

If you use precompile then use noEscape option:

handlebars.precompile(content, {noEscape: true})
Solution courtesy of: Poelinca Dorin

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

node.js and Handlebars: HTML compiled is escaped

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×