我使用它来“获取”我的外部 html 文件,然后使用 Mustache 附加到该模板的 ID:
$.get('block.html', function(data) {
$('#mydiv').append(data);
var list = {
name : 'whatever'
};
$('#Block').mustache(list).appendTo('#mydiv');
});
文件block.html
看起来像:
<script id="Block" type="x-tmpl-mustache">
My name is {{name}}
</script>
有更好的方法吗?因为目前我要追加两次。
最佳答案
嗯,当模板位于当前文档中时,jquery Mustache 插件非常有用。
但是这里有一个不同的用例,并且 Mustache 本身提供的帮助器足以完成这项工作。所以,只需:
$.get('block.html', function(template) {
var view = {name:'whatever'};
var html = Mustache.to_html(template, view);
// and now append the html anywhere you like
});
在这种情况下,您的 block.html 可以变成:
My name is {{name}}
关于jquery - 如何将jquery Mustache应用到外部文件/模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7780855/