0

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?

2 Answers 2

4

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 });
Sign up to request clarification or add additional context in comments.

4 Comments

whoaa, ok, that's weird syntax, that got resolved, but now it says ops is not defined
Still not working, getting the same error, my script files are loading after #test_temp, is that correct or do I need to change that?
Shouldn't it be { ops: ops }, not { opts: ops }?
@Lukas: Right, thanks, my fingers don't always type what my brain thinks they should :)
0

I got a similar error

Uncaught SyntaxError: Unexpected token var at new Function () at Function._.template

that's because in my template I wrote a js statement inside <%= instead of <%

so make sure the syntax of your template is correct

Comments

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.