我开始学习mustache.js,但我试图加载外部模板(template.html)。下面的代码工作正常。但我不喜欢" $。得到(' template.html',... ..."方法。有更短更便宜的方式。例如:< link href =" path / to / template.mustache" rel =" template" id =" templateName" />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<h2>Titles</h2>
<ul id="talktitles">
</ul>
<script src="mustache.js"></script>
<script src="jquery.js"></script>
<script>
$(function() {
$.getJSON('/data/speakers.json', function(data) {
//var template = $('#speakers-template').html();
// console.log(template);
// var info = Mustache.render(template, data);
// $('#talktitles').html(info);
$.get('template.html', function(template, textStatus, jqXhr) {
var info =Mustache.render($(template).filter('#speakers-template').html(), data);
$('#talktitles').html(info);
});
});
});
</script>
</head>
<body>
<div id="talktitles"></div>
</body>
</html>