Reputation: 5183
I am using underscore with backbone in a jsp app.
In order to do that, I have used:
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
in order to have mustache-like templates so as to avoid jsp conflict.
Until this point everything is ok (i.e. filled correctly templates with simple values).
Now, I am trying to add a for loop inside the template in order to show a json array.
So I added this code:
<script type="text/template" id="im-template">
<div class="row">
<div class="horizontal-scroll">
{{ for (var i = 0; i < data.length; i++) {
var template = data[i];
alert(template.name);
} }}
</div>
</div>
</script>
But when running the app, I get:
Uncaught SyntaxError: Unexpected token for
Do you know what could be the problem or how could I solve it?
Thanks in advance!
Upvotes: 1
Views: 1507
Reputation: 434665
You're replacing the wrong regex, you want to replace the evaluate regex not the interpolate regex. From the fine manual:
Define an interpolate regex to match expressions that should be interpolated verbatim, an escape regex to match expressions that should be inserted after being HTML escaped, and an evaluate regex to match expressions that should be evaluated without insertion into the resulting string.
Interpolation is used to replace a template token with the result of a JavaScript expression but a for
loop is not an expression in JavaScript.
You want to replace the evaluate regex if you want to use a for
loop like that:
_.templateSettings.evaluate = /\{\{(.+?)\}\}/g;
Demo: http://jsfiddle.net/ambiguous/gaYRb/
Upvotes: 3