Underscore.js with node-express

I am trying to use underscore.js in an HTML file hosted on a node-express server. This file is used to render dynamic data on the client side. Below are the codes:

in app.js:

var cons = require('consolidate');
app.engine('html', cons.underscore);
app.set('view engine', 'html');
app.locals._ = require("underscore");

      

in HTML:

var template = _.template($('#client-list-template').html(), {clients: response});

      

here "response" is JSON

in the same HTML under the script template

<script id ="client-list-template", type='text/template'>
        <table class="table striped">
            <thead>
                <tr>
                    <th>ID</th><th></th>
                </tr>
            </thead>
            <tbody>
                <% _.each(clients, function(client) { %>
                    <tr>
                        <td><%= client.clientID %></td>
                        <td><a class="btn">Edit</a></td>
                    </tr>
                <% }); %>
            </tbody>
        </table>
    </script>

      

runs this code and throws errors:

ReferenceError: clients is not defined

Can someone please help me understand what the error is and how to solve it. The same HTML hosted on Apache server works great.

+3


source to share


1 answer


Have you tried it like this?

var template = _.template($('#client-list-template').html());
$('.output-div').html(template({
     clients : response
});

      



Also how do you get the response variable?

+1


source







All Articles