【发布时间】:2016-01-05 17:44:57
【问题描述】:
我正在尝试在 HTML 模板中使用 lodash 来处理 Node.js 中的电子邮件。我有一个包含多个对象的数组。我想遍历每个对象并列出所有重复值。当我使用下面的代码时,我收到一条错误消息,指出该值未定义(例如,ReferenceError: firstName is not defined)。 HTML 模板位于单独的文件中。
对我做错了什么有什么想法吗?
Javascript:
var template = fs.readFileSync('server/views/email-template.html').toString();
var htmlAll = _.template(template)(orderInfo);
HTML:
<% _.forEach(function(firstName) { %><%- firstName %></td><% }); %> <% _.forEach(function(lastName) { %><%- lastName %></td><% }); %>
<% _.forEach(function(address) { %><%- address %></td><% });%>
<% _.forEach(function(city) { %><%- city %><% }); %>, <% _.forEach(function(state.code) { %><%- state.code %><% });
%> <% _.forEach(function(zip) { %><%- zip %><% }); %>
<% _.forEach(function(item) { %><td><%- item %></td><% }); %>
<% _.forEach(function(cost) { %><td><%- cost %></td><% }); %>
数组:
[
{
"firstName": "John",
"lastName": "Doe",
"address": "123 Broadway",
"city": "New York",
"state": {
"code": "NY",
"state": "New York"
},
"zip": "10001",
},
{
"color": "White",
"size": "M",
"item": "T-Shirt",
"cost": 19.99,
},
{
"color": "Blue",
"size": "L",
"item": "T-Shirt",
"cost": 19.99,
}
]
【问题讨论】:
-
这些模板是 EJS 吗?如果是这样,请相应地标记您的问题。
-
@m01 不,这只是 underscore.js 模板
标签: node.js underscore.js lodash