3

I am trying to use underscore.js in a HTML file hosted with node-express server. This file is being used to render the dynamic data on 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 a JSON

in same HTML under template script

<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>

running this code throws and error:

ReferenceError: clients is not defined

Can someone please help me understanding what is the error and how to resolve it. Same HTML when hosted in Apache server works fine.

4
  • have you logged response? it looks like it may be undefined Commented Dec 19, 2014 at 23:59
  • 2
    _.template doesn't take two arguments. It takes one (the template) and returns a function, which itself takes one argument (the data). Commented Dec 20, 2014 at 0:01
  • 1
    @Jack It does actually take two arguments, but the second is not the data, it's the settings for the template string. But yeah, Vinny, you should do what he says. Commented Dec 20, 2014 at 0:46
  • Well, are you passing clients into the template as data? Commented Jan 14, 2015 at 10:46

1 Answer 1

1

Did you try it this way?

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

Also how are you getting the response variable?

Sign up to request clarification or add additional context in comments.

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.