Mustache: Globally disable html escaping?

纵然是瞬间 提交于 2019-12-22 01:28:51

问题


Is there a way how I can (without modifying the sources of mustache) disable the HTML escaping? I'm using mustache for other things and dont want to have the following entities escaped.

var entityMap = {
  "&": "&",
  "<": "&lt;",
  ">": "&gt;",
  '"': '&quot;',
  "'": '&#39;',
  "/": '&#x2F;'
};

Given a template like foo '{{bar}}' and a view { bar : 1 }will produce foo &#39;1&#39.


回答1:


If you are trying to just NOT HTML escape some strings, you just do {{{xx}}} instead of {{xx}}

As per:

http://mustache.github.io/mustache.5.html

So if you had a string that consisted of:

test => Q & A

Calling with:

{{ test }}

would give you:

Q &amp; A

..but calling with:

{{{ test }}} or {{ &test }}

Would give you just:

q & a



回答2:


It's actually pretty simple, mustache is offering the escape function to be overridden. The following line disables the escaping of mustache templates.

mustache.escape = function (value)
{
    return value;
};


来源:https://stackoverflow.com/questions/22910428/mustache-globally-disable-html-escaping

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