我正在尝试使用 mustache.js 进行模板化,但我的基本代码无法正常工作请帮助我出错的地方-
var person = {
firstName: "Christophe",
lastName: "Coenraets"
};
var template = "<h1>{{firstName}} {{lastName}}</h1>";
var output = Mustache.render(template, person);
document.getElementById('result1').innerHTML = output;
上面的代码有效,但下面的代码无效:-
这一行写在我的 .html 页面中:
<script id="sample_template" type="text/template">
<h1>{{firstName}} {{lastName}}</h1>
</script>
这一行写在我的 .js 文件中:
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);
最佳答案
我得到了解决方案,我有 mustache 服务器端和客户端,我上面描述的问题是,当我尝试在客户端填充占位符时,这些占位符已经在服务器端使用了(因为我没有单独使用必须呈现服务器端和呈现客户端的文件)。所以他们在客户端渲染时没有占位符,所以只有我得到的 html 标签才会显示。
关于javascript - mustache.js 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21883079/