17

I have an unusual situation where a client would like to place a line break in a string that is passed to handlebars from a JSON object. I've tried escaping characters but it isn't rendered by the DOM unsurprisingly. Any suggestions?


"company": "Lorem adscs ireland <br/> marketed as iuhmdsf in Europe"

var products = Data;

var theTemplateScript = $("#product-template").html();

var theTemplate = Handlebars.compile (theTemplateScript);
$("#marketed-products .products").append (theTemplate(products));

{{#items}}
<li><span class="company">{{company}}</li>
{{/items}}

The output from the code above should look something like this

Lorem adscs ireland
marketed as iuhmdsf in Europe

Zach Shallbetter
  • 1,761
  • 5
  • 22
  • 37
  • 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:18

1 Answers1

44

Use triple brackets, like {{{returnedHtml}}}, in that case Handlebars will not escape the value.

i.e it will become:

{{#items}}
<li><span class="company">{{{company}}}</li>
{{/items}}
artahian
  • 2,015
  • 12
  • 16