Reputation: 455
I have a Name
and Status
fields on my table and I want to display the values, Active and Inactive for the Status field. Here is the template I'm using:
<tbody>
<% _.each(accountLists, function(account) { if (account.active == 'true') ? 'Active': 'Inactive'%>
<tr>
<td><%= account.active %></td>
</tr>
<% }) %>
</tbody>
When I run, the template throws:
Uncaught SyntaxError: Unexpected token
Why?
For reference, below is my accountView.js
var AccountList = Backbone.View.extend({
initialize: function(){
},
el:'#sub-account-list',
render: function(id){
var self = this;
var accountList = new SubAccountCollection([],{ id: id });
accountList.fetch({
success: function(accountLists){
var data = accountLists.toJSON();
var accounts = data[0].data.items;
var template = $("#sub-account-list").html(_.template(tmpl, {accounts:accounts}));
},
});
}
});
Upvotes: 1
Views: 13096
Reputation: 664484
This doesn't have much to do with underscore templates - it will translate roughly into:
_.each(accountLists, function(account) {
if (account.active == 'true') ? 'Active': 'Inactive'
echo ("<tr><td>" + account.active "</td></tr>");
})
I'm not sure what you wanted to do here, but this is horribly mixing the if statement with the conditional operator syntax. Use either
<tbody>
<% _.each(accountLists, function(account) {
if (account.active == 'true') { %>
<tr>
<td>Active</td>
</tr>
<% } else { %>
<tr>
<td>Inactive</td>
</tr>
<% }
}); %>
</tbody>
or
<tbody>
<% _.each(accountLists, function(account) { %>
<tr>
<td><%= (account.active == 'true') ? 'Active': 'Inactive' %></td>
</tr>
<% }); %>
</tbody>
Upvotes: 6