Handlebars.js disable escaping with noEscape option?

那年仲夏 提交于 2019-12-09 14:52: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


回答1:


Try something like this:

var template = Handlebars.compile(source, {noEscape: true});



回答2:


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.




回答3:


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/



来源:https://stackoverflow.com/questions/9860135/handlebars-js-disable-escaping-with-noescape-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!