问题
So I want to be able to do something like this:
"{{tickets." + index + ".comments.0.account}}"
I want to be able to specify the index of the array that I am trying to get data from with a javascript variable. Currently this code gives me an error which says:
Expecting 'ID', got 'STRING'
I am actually using express-handlebars with Node.js if this makes a difference.
回答1:
Create a helper:
Handlebars.registerHelper('getTicketAccount', function(context, i) {
// Error handling ommitted for brevity
return context[i].comments[0].account;
});
To use:
{{getTicketAccount tickets index}}
来源:https://stackoverflow.com/questions/30412591/handlebars-using-a-javascript-variable-for-index