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>
You have two problems:
templateSettings
regexes overlap in a bad way.templateSettings
don't match your template.The documentation isn't explicit about what order the regexes are applied in but the source code is:
In particular,
interpolate
will be matched beforeevaluate
. Yourinterpolate
is looking for things like{{ ... }}
so it will pick up things like{{= ... }}
before yourevaluate
regex can see them. The result will be stray=
in the generated source code. So yourinterpolate
regex can't match things that yourevaluate
will be looking for. You probably want to use these regexes:Note that I've switched who looks for
{{= ... }}
and{{ ... }}
.Then your template needs to use the Mustache-style delimiters rather than the default Underscore ones:
Fixing those two issues should make things work.
Demo: http://jsfiddle.net/ambiguous/my49N/