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.
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.
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).
Mustache is a logicless template engine for creating dynamic content like HTML, configuration files among other things.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With