I'm trying to learn Backbone.js with Underscore.js and got into trouble. I'm using Grails as a server framework, so the Underscore.js syntax <%= %> is not possible. I want to change it to {{}} style. My Javascripts are separated in many files, each representing either a View or Model for every objects I need. Here is the code for my View:
$(function () {
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g,
evaluate : /\{!(.+?)!\}/g
};
APP = window.APP || {};
APP.PlaceView = Backbone.View.extend({
initialize:function () {
this.render();
},
el:"#place-form",
formTemplate:_.template($('#search-template').html()),
render:function () {
//Pass variables in using Underscore.js Template
var variables = { street:"Ulica" };
this.$el.html(this.formTemplate({ "street":"Ulica" }));
}
});
var view = new APP.PlaceView();
});
And the template:
<script type="text/template" id="search-template">
<!-- Access template variables with {{ }} -->
<label>{{ street }}</label>
<input type="text" id="search_input"/>
<input type="button" id="search_button" value="Search"/>
</script>
This code throws Uncaught SyntaxError: Unexpected token ) error. But when I delete the _.templateSettings part, everything is ok, but I don't have the variables.
Thanks a lot.