içinde dizinin içindeki nesneler arasında yineleme nasıl Node.js. bir e-posta için bir HTML şablonu içinde lodash kullanmaya çalışıyorum Birkaç nesneye sahip bir dizim var. Her bir nesneyi tekrarlamak ve yinelenen tüm değerleri listelemek istiyorum. Aşağıdaki kodu kullandığımda, değerin undefined olduğunu belirten bir hata alıyorum (ör. ReferenceError: firstName is not defined
). HTML şablonu ayrı bir dosyada.Lodash
Neyi yanlış yaptığım hakkında bir fikrin var mı?
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><% }); %>
Dizisi: Şablonunuza orderInfo[0]
vermek çünkü
[
{
"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,
}
]
Bu şablonlar EJS? Öyleyse, lütfen sorunuza uygun şekilde etiketleyin. – mik01aj
@ m01 hayır bu sadece underscore.js şablonları – Magus