Mustache template string inside render as HTML

JavascriptJqueryHtmlMustache

Javascript Problem Overview


I am using Mustache to render templates.

I have this json object:

  {
    title: "Foo bar", 
    content: "<p> Html here </p>", 
    footer: "footer content here"
  }

I have a Mustache template like:

  <div id="box">
    <div id="title"> {{title}} </div> 
    <div id="content"> {{content}} </div> 
    <div id="footer"> {{footer}} </div> 
  </div>

My problem is that the html within the variable content is not getting rendered but instead is just getting printed to the screen.

I see (in non-view source window): <p> Html here </p>, where I would only want to see that if I viewed the page source.

How can I fix such that when I pass in a string to a mustache template the HTML inside gets rendered? I am calling mustache.render(templates.all,data); as my call to mustache.

Javascript Solutions


Solution 1 - Javascript

From the Mustache documentation:

> All variables are HTML escaped by default. If you want to return > unescaped HTML, use the triple mustache: {{{name}}}.

So you just have to use eg.{{{content}}} in your template:

  <div id="box">
    <div id="title"> {{title}} </div> 
    <div id="content"> {{{content}}} </div> 
    <div id="footer"> {{footer}} </div> 
  </div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSnow_MacView Question on Stackoverflow
Solution 1 - JavascriptAmyView Answer on Stackoverflow