当我尝试呈现以下模板时,在运行时出现编译错误:
<script id="tmpl-books" type="text/template">
<% _.each(items, function(item) { %>
<ul>
<li>Title: <%= item.title %></li>
<li>Author: <%= item.author %></li>
</ul>
<% }); %>
</script>
<script type="text/javascript">
_.templateSettings = {
evaluate: /\{\{=(.+?)\}\}/g,
interpolate: /\{\{(.+?)\}\}/g,
escape: /\{\{-(.+?)\}\}/g
};
var list =
{
items:
[
{ "title": "Myst: The Book of Atrus", "author": "Rand Miller" },
{ "title": "The Hobbit", "author": "J.R.R. Tolkien" },
{ "title": "Stardust", "author": "Neil Gaiman" }]
};
$(document).ready(function () {
var tmplMarkup = $('#tmpl-books').html();
// ...tell Underscore to render the template...
var compiledTmpl = _.template(tmplMarkup, list);
// ...and update part of your page:
$('#rendered').append(compiledTmpl);
});
</script>