访问 handlebars.js 中的下一个数组项

Access to next array item in handlebars.js

在 handlebars.js.I 中使用 foreach 循环时,我需要访问数组中的下一项已尝试:

{{#each items}}
{{#ifEven @index}}
<p>{{this.description}} - {{../items.[@index+1].description}}</p>
{{/ifEven}}
{{/each}}

但似乎无法正常工作。

我已经用助手解决了它:

hbs.registerHelper('nextItem', function (array, index, options) {
  return options.fn(array[index + 1]);
});

.hbs 模板现在看起来像这样:

{{#each items}}
{{#ifEven @index}}
<p>{{this.description}} - {{#nextItem ../items index}} {{description}} {{/nextItem}}</p>
{{/ifEven}}
{{/each}}

我知道,最后一个元素不会显示,但这对我来说无关紧要。