Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustache template string inside render as HTML

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.

like image 649
Snow_Mac Avatar asked May 28 '13 20:05

Snow_Mac


People also ask

How do I render a mustache in HTML?

render = function (template, view, partials) { return this. compile(template)(view, partials); }; This is the most basic form of templating with mustache. Let's see the other methods available for creating more organized code.

Why use mustache template?

Mustache is described as a logic-less system because it lacks any explicit control flow statements, like if and else conditionals or for loops; however, both looping and conditional evaluation can be achieved using section tags processing lists and anonymous functions (lambdas).

Is mustache a template engine?

Mustache is a logicless template engine for creating dynamic content like HTML, configuration files among other things.


1 Answers

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> 
like image 87
Amy Avatar answered Oct 16 '22 09:10

Amy