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!
You’re replacing the wrong regex, you want to replace the evaluate regex not the interpolate regex. From the fine manual:
Interpolation is used to replace a template token with the result of a JavaScript expression but a
forloop is not an expression in JavaScript.You want to replace the evaluate regex if you want to use a
forloop like that:Demo: http://jsfiddle.net/ambiguous/gaYRb/