简体   繁体   中英

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);  

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.

从文档中,似乎不推荐使用.to_html() ......

I had the same issue as level_0 . Django templating on the server-side also uses the same tag as mustache-js ie {{ }}

Here is a simple example that gets past the Django server-side templating issue.

<!DOCTYPE HTML>
<html>

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.3.0/mustache.js"></script>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
</head>

<body>
    <div id="target">Loading...</div>
    <script id="template" type="x-tmpl-mustache">
        Hello {% templatetag openvariable %} name {% templatetag closevariable %}
    </script>
    <script>
    $(document).ready(function() {

        var template = $('#template').html();
        console.log(template);
        Mustache.parse(template); // optional, speeds up future uses
        var rendered = Mustache.render(template, { name: "Luke" });
        $('#target').html(rendered);

    });
    </script>
</body>

</html>

If you ended up in this thread as I did, because you googled for "Mustache.to_html is not a function", you can simply replace Mustache.to_html with Mustache.render . The first one has been removed, so you probably updated the library.

This works for me: http://jsfiddle.net/evildonald/qK5NT/

js:

$(document).ready(function () {
  var data ={
     firstName: "Christophe",
     lastName: "Coenraets"
  };

  var template = $('#sample_template').html();

  var info = Mustache.to_html(template, data);

  $('#result1').html(info); 
});

html:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM