I get a compilation error at runtime when I attempt to render the following template:
<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>