3

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.

1
  • I'm getting the exact same error, I'm using handlebar's syntax because I'm using HAML, any idea how you solved this? Commented Jan 10, 2013 at 15:58

1 Answer 1

10

I believe it's showing that error because of this line:

<!-- Access template variables with {{ }} -->

Underscore is trying to replace that with a variable that doesn't exist, thus throwing an error.

Sign up to request clarification or add additional context in comments.

1 Comment

this was driving me crazy! It shouldn't be in the sample code </rant>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.