Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustache: Globally disable html escaping?

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 = {   "&": "&amp;",   "<": "&lt;",   ">": "&gt;",   '"': '&quot;',   "'": '&#39;',   "/": '&#x2F;' }; 

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

like image 374
Christopher Klewes Avatar asked Apr 07 '14 10:04

Christopher Klewes


People also ask

What is mustache in HTML?

A Mustache tag begins with two opening braces ( {{ ) and ends with two closing braces ( }} ). As you might have guessed, the {{ and }} delimiters are where Mustache gets its name from!

What does escaping HTML do?

Escaping in HTML means, that you are replacing some special characters with others. In HTML it means usally, you replace e. e.g < or > or " or & . These characters have special meanings in HTML. And the text will appear as hello, world.

What is mustache syntax?

Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. We call it "logic-less" because there are no if statements, else clauses, or for loops.


2 Answers

It's actually pretty simple. Mustache offers the possibility to override the escape function. This allows you to disable the escaping by simply returning the original value.

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

As mentioned by others, you can also use the following notation to disable escaping.

{{{ test }}} 

I leave the answer unchanged, since it might be helpful to implement your own sanitizing.

like image 40
Christopher Klewes Avatar answered Oct 19 '22 13:10

Christopher Klewes


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 
like image 70
Andrew Newby Avatar answered Oct 19 '22 12:10

Andrew Newby