[英]Example of Node.js Express registering Underscore.js as view engine?
[英]underscore.js with node-express
我試圖在使用node-express服務器托管的HTML文件中使用underscore.js。 此文件用於在客戶端呈現動態數據。 以下是代碼:
在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});
這里“響應”是一個JSON
在模板腳本下的相同HTML中
<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工作正常。
你這樣試試嗎?
var template = _.template($('#client-list-template').html());
$('.output-div').html(template({
clients : response
});
你是如何獲得響應變量的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.