Sto provando ad utilizzare lodash all'interno di un modello HTML per un messaggio di posta elettronica in Node.js. Ho una matrice con diversi oggetti. Vorrei scorrere ogni oggetto ed elencare tutti i valori ripetuti. Quando utilizzo il codice riportato di seguito, viene visualizzato un errore che indica che il valore non è definito (ad es., ReferenceError: firstName is not defined
). Il modello HTML è in un file separato.Come eseguire iterazioni su oggetti all'interno dell'array in Lodash
Qualche idea su cosa sto facendo male?
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><% }); %>
Array:
[
{
"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,
}
]
Questi modelli sono EJS? Se è così, tagga la tua domanda di conseguenza. – mik01aj
@ m01 no è solo underscore.js templates – Magus