17

I have all my content pre-escaped, so rather than using the triple stash everywhere i would like to globally disable handlebars escaping. A quick search showed a similar feature which I can see in my build of handlebars, however I don't know how to turn it on.

The pull request is here: https://github.com/wycats/handlebars.js/pull/121

I've tried adding Handlebars.Compiler.options.noEscape = true in my code but it always comes back with options undefined. Even after defining the options its not picking it up. Does anyone know how I should be enabling this option in my script file? Thanks

ant-depalma
  • 2,006
  • 4
  • 26
  • 34

5 Answers5

42

Try something like this:

var template = Handlebars.compile(source, {noEscape: true});
Juho Vepsäläinen
  • 26,573
  • 12
  • 79
  • 105
11

Using the "triple-stash" {{{ is another option when you only want one variable in the template to not get escaped:

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

https://handlebarsjs.com/

Rvanlaak
  • 2,971
  • 20
  • 40
  • This is exactly what I was looking for -- only had one value to unescape, turning it off entirely seemed like overkill. Thanks for surfacing that option from the docs! – ohsully Mar 20 '19 at 19:22
  • This is very useful since we dont wish to escape all files, only a part. – NingaCodingTRV Apr 09 '22 at 02:21
5

Suppose,

var template = "This is {{target}}";
var target = "user's pictures";
var result = Handlerbars.compile(template, {noEscape:true})({target:target});

Now try to print result. There is an apostrophe in target string value. Which will not change by encoded string. If you will remove the {noEscape:true}from compile function then it will change.

SatyamChaudhary
  • 381
  • 2
  • 8
0

{noEscape:true} did not complile the template properly when tried with handlebarsv4.0.5.

'{{{' works perfectly for me.

thiagu
  • 13
  • 3
0

This is the correct syntax:

var handleBars = Handlebars.Create(new HandlebarsConfiguration { NoEscape = true });

var template = handleBars.Compile(sharingTemplate.Template);

var result = template(items);

Biraj Saha
  • 130
  • 1
  • 5