Search code examples
backbone.jsunderscore.jstemplate-engine

Error 'Uncaught SyntaxError: Unexpected token < in underscore.js'


Here's my code,

HTML:

<script type="text/html" id="test_temp">
    <div class="row" id="opportunityList">
        <% _.each(ops, function(option){
            <div class="span12">
                <div class="well basicInformation">
                    <div class="row">
                        <div class="span4 opportunityName" >
                            <h4><%= option.company_name %></h4>
                        </div>
                        <div class="span4 pull-right">
                            <ul class="inline">
                                <li class="dealGrade">
                                    <span><%= option.dealGrade %></span>
                                    <span class="subheader">Deal Grade</span>
                                </li>
                                <li class="estimateddevices">
                                    <span><%= option.devices %></span>
                                    <span class="subheader">Devices</span>
                                </li>
                                <li class="accountValue">
                                    <span><%= option.accountValue %></span>
                                    <span class="subheader">Account value</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        });
        %>
    </div>
</script>

SCRIPT:

testView = Backbone.View.extend({
    initialize: function(){
        this.render();
    },

    render: function(){
        var ops = [
            {dealGrade: '50%', devices: 123, accountValue: '20%', company_name: 'Kyocera', rep_name: 'James Kogg', rep_designation: 'Sales Rep', proposalCount: 2},
            {dealGrade: '75%', devices: 215, accountValue: '41%', company_name: 'Flipkart', rep_name: 'Christina Kogg', rep_designation: 'MD', proposalCount: 0}
        ]

        var template = _.template($("#test_temp").html(), ops);

        this.$el.append(template);
    }
});

var test_view = new testView({ el: $("#viewport .container") });

I get this error message:

Uncaught SyntaxError: Unexpected token < in underscore.js

What am I doing wrong?


Solution

  • You're not closing the <% around the _.each or opening the template tag for the _.each's closing });:

    <script type="text/html" id="test_temp">
       <div class="row" id="opportunityList">
         <% _.each(ops, function(option){ %>
           ...
         <% }); %>
       </div>
    </script>
    

    Underscore's template engine is pretty simple minded, it just does a bit of simple text wrangling to turn your template inside-out into JavaScript code.

    Also, the template function wants its data as key/value pairs (i.e. a JavaScript object) so you'll need to give your ops a name:

    var template = _.template($("#test_temp").html(), { ops: ops });