Maverick Maverick - 1 year ago 191
Javascript Question

Handlebars HTML entity decode

I'm using the handlebars templating engine on the app Im building to render the data I get from the server.

I know that it escapes HTML values by default, you have to use the triple brackets

in order for
text: <p>Example</p>
to be rendered as an HTML element.

The problem is, what do I do if the data I receive, including the HTML tags, is already escaped.

So, if I receive date like:

text: &lt;p&gt;Example&lt;/p&gt;

How do I force handlebars to translate it and render it as normal HTML?

Answer Source

You have to decode it first, then pass it to handlebars with triple brackets. I know a small tip to decode html entities with jQuery:

// encoded is "&lt;p&gt;Example&lt;/p&gt" in your example
var decoded = $('<textarea />').html(encoded).val();
// decoded should now return <p>Example</p>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download