Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Unexpected token ')' in underscore when looping throught template

I have a problem when using underscore in Backbone application. In console I have

Uncaught SyntaxError: Unexpected token ')'

And it referer me to the underscore library :

underscore.js:line 1443

What I whant to do is select template by id

  var UserList = Backbone.View.extend({
               el: '.page',
               render: function(){
                   var self = this;
                   var users = new Users();
                   users.fetch({
                      success:function(users){
                          console.log(users);
                          var template = _.template($('#user_list_template').html(), users);
                          self.$el.html(template); 
                      } 
                   });
               }
            });

Here is my script template

<script type="text/template" id="user_list_template">

            <table class="table striped">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                <% _.each(users,function(user)){ %>
                    <tr>
                        <td><%= user.name %></td>
                        <td><%= user.age %></td>
                    </tr>
                <% }); %>
            </tbody>
            </table>

        </script>

And as I found, the problem is in this line:

   var template = _.template($('#user_list_template').html(), users);

Could you help me please to find what is the problem?

like image 875
volodymyr3131 Avatar asked Feb 10 '23 07:02

volodymyr3131


1 Answers

<% _.each(users,function(user)){ %> this line has an extra ) before the { in your template.

like image 165
GillesC Avatar answered Feb 12 '23 21:02

GillesC