3
我想在使用node-express服务器托管的HTML文件中使用underscore.js。该文件正用于在客户端呈现动态数据。下面是代码:underscore.js与node-express
在app.js:
var cons = require('consolidate');
app.engine('html', cons.underscore);
app.set('view engine', 'html');
app.locals._ = require("underscore");
在HTML:
var template = _.template($('#client-list-template').html(), {clients: response});
这里 “响应” 是相同的HTML一个JSON
下模板脚本
<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>
运行此代码抛出和错误:
ReferenceError: clients is not defined
有人可以帮助我了解什么是错误以及如何解决它。在Apache服务器中托管的相同HTML工作正常。
你记录了'response'吗?它看起来可能是未定义的 –
'_.template'不带两个参数。它需要一个(模板)并返回一个函数,该函数本身需要一个参数(数据)。 – Jack
@Jack它确实[有两个参数](http://underscorejs.org/#template),但第二个不是数据,它是模板字符串的设置。但是,温尼,你应该做他说的。 – jakerella