211

Is there a way to insert a string with html tags into a handlebars template without getting the tags escaped in the outcoming string?

template.js:

<p>{{content}}</p>

use the template

HBS.template({content: "<i>test</i> 123"})

actual outcome:

<p>&lt;i&gt;test&lt;/i&gt; 123</p>

expected result:

<p><i>test</i> 123</p>
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
  • Possible duplicate of [Handlebars Template rendering template as text](https://stackoverflow.com/questions/7168469/handlebars-template-rendering-template-as-text) – MathKimRobin Jul 03 '19 at 14:17

3 Answers3

492

Try like

<p>{{{content}}}</p>

official reference:

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

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • 13
    Honestly seems like the worst thing they could've chosen since it's hard to notice. Why not just something simple but visible like `{{ rawHtml expression }}`. – danneu Jan 29 '17 at 14:03
  • 2
    Agree with @danneu. No magic or cute shorthand, please. Explicit is better in the long run. – Matt Jan 08 '20 at 20:03
  • 5
    It is worth noticing that spaces also matters, so `{{{content}}}` != `{{{ content }}}`, second will not work. Be sure you do not include any spaces. – bladekp Dec 19 '21 at 16:24
40

In your template you must add triple mustaches like this. <p>{{{content}}}</p>

See Official Reference for more information on that.

Float07
  • 314
  • 2
  • 12
Jernej Novak
  • 3,165
  • 1
  • 33
  • 43
30

According to the Handlebars documentation:

If you don't want Handlebars to escape a value, use the "triple-stash", {{{

Pass the raw HTML to Handlebars template and get the raw HTML output by using triple brackets.

{{{foo}}}
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
XY L
  • 25,431
  • 14
  • 84
  • 143