最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to write a conditional if statement using underscore template - Stack Overflow

programmeradmin0浏览0评论

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}));

                },
            });
        }
    });

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}));

                },
            });
        }
    });
Share Improve this question edited Jun 14, 2014 at 21:43 Kate Gregory 19k8 gold badges59 silver badges86 bronze badges asked Jun 13, 2014 at 3:03 user2942566user2942566 4554 gold badges10 silver badges22 bronze badges 2
  • i understand i should have added this question to my existing question before, but anyways, i resolved my issue i was facing. Thanks for the help..however i am stuck at this another issue, im trying to resolve on how can i write conditional statements in underscore template. Above is the code i have edited to show what im trying to achieve.. – user2942566 Commented Jun 14, 2014 at 20:49
  • Possible duplicate of How to use if statements in underscore.js templates? – T J Commented Jun 28, 2016 at 10:03
Add a ment  | 

1 Answer 1

Reset to default 6

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>
发布评论

评论列表(0)

  1. 暂无评论