I want to do something like this:
<script id="tmpl-books" type="text/template">
<ul>
<% for (var i = 0; i < books.length; i++) { %>
<% var book = books[i]; %>
<li>
<em><%= book.title %></em> by <%= book.author %>
</li>
<% } %>
</ul>
</script>
in my code, but I am using JSP so I have to use the {{ }} notation, but when I do this {{ for(var... }} does not work.
<script id="tmpl-books" type="text/template">
<ul>
{{ for (var i = 0; i < books.length; i++) { }}
<% var book = books[i]; %>
<li>
<em>{{= book.title }}</em> by {{ book.author }}
</li>
{{ } }}
</ul>
</script>
How can I achieve this?
Underscore templates have three distinct regexes:
If you want to use Handlebars-ish delimiters, you'll want to define all three rather than just the
interpolate
regex:Also note that the
interpolate
andescape
regexes must match things thatevaluate
won't due to the order that the regexes are applied in.With those template settings, a template like this:
should work. Note the change from
{{ book.author }}
to{{= book.author }}
since you're interpolatingbook.author
, not evaluating it.