5
我正在阅读有关Mustache.js的模板。我不明白的是如何放置模板。我不会在同一个文件中拒绝他们。使用外部模板的小胡子
$.get('test.htm', function(templates) {
// Fetch the <script /> block from the loaded external
// template file which contains our greetings template.
var template = $(templates).filter('#tpl-greeting').html();
$('body').append(Mustache.render(template, templateData));
});
//test.htm
<script id="tpl-greeting" type="text/html">
<dl>
<dt>Name</dt>
<dd>{{name}}</dd>
<dt>Time</dt>
<dd>{{timeNow}}</dd>
</dl>
</script>
我必须创建一个控制器,返回我的模板或我可以参考吗?
谢谢您的回答,但后来我不明白我的意思。为什么不让控制器返回一个“填充的”html并且做一个$(“#old”)。replaceWith(“#new”); – pethel
@ user874774你绝对可以。但是您仍然需要构造新的HTML w/new Data值以在replaceWith方法中使用。模板为你做这件事,再加上使用模板可以保证一致性和清洁度。 – Swordfish0321