Handlebars: using a javascript variable for index

一笑奈何 提交于 2021-02-08 09:22:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!