12

I have JSON like this:

 { "something": "http://something.com" }

and HTML like this:

 <a href="{{something}}">{{something}}</a>

When I apply Mustache, I get

 <a href="%7B%7Bsomething%7D%7D">http://something.com</a>

But what I am trying to get is

 <a href="http://something.com">http://something.com</a>

I already tried {{{ something}}}, {{& something}}, single quotes, double quotes... I even read documentation.

Can you help me?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Evgeny
  • 6,261
  • 8
  • 35
  • 43

2 Answers2

16

I think you need to make use of the & for escaping in combination with surrounding your template with a template script:

<script type="text/template" id="tmpl">
    <a href="{{& something }}">{{ something }}</a>
</script>

Found this example over here.

Community
  • 1
  • 1
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
5

Make sure your template source is straight text - don't try and grab parsed HTML source as your template. Browsers will urlencode/escape those characters in your link href, and result in those %7Bs and %7Ds you see in your code. Mustache won't recognize that.

I suppose unescaping the source you pass to mustache might work, though.

Mustache.render(unescape(source),view)
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Sean Johnson
  • 5,567
  • 2
  • 17
  • 22
  • Thanks! Unescaping actually helped. Turns out I was using jQuery to do some transformation with template before feeding it to Mustache (I basically did something like this: $("{{something}}") ), that's why this piece of HTML become escaped – Evgeny Jun 29 '12 at 20:48