Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mustache.js is not working

I am try to use mustache.js for templating but my basic code is not working Pls help where I m going wrong-

var person = {
    firstName: "Christophe",
    lastName: "Coenraets"
};

var template = "<h1>{{firstName}} {{lastName}}</h1>";
var output = Mustache.render(template, person);
document.getElementById('result1').innerHTML = output;

The above code is working but the below code is not working :-

This line is written in my .html page:

<script id="sample_template" type="text/template">
    <h1>{{firstName}} {{lastName}}</h1>
</script>

This line is written in my .js file:

var data ={
   firstName: "Christophe",
   lastName: "Coenraets"
};
var template = $('#sample_template').html();
//console.log(template);  this prints the html content in console
var info = Mustache.to_html(template, data);
$('#result1').html(info);  
like image 460
level_0 Avatar asked Dec 06 '25 02:12

level_0


2 Answers

I got the solution i have both mustache server side and client side and the problem i describe above is that when i try to fill the placeholders at client side then those placeholder are already consume at server side(becoz i didn't sperate file which has to render server side and whom to render client side ). So their is no placeholder while rendering on client side so only i got html tags will display.

like image 153
level_0 Avatar answered Dec 08 '25 14:12

level_0


From the documentation, it seems that .to_html() is deprecated...

like image 30
Ronen Cypis Avatar answered Dec 08 '25 15:12

Ronen Cypis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!