我正在使用 Handlebars for java:https ://github.com/jknack/handlebars.java 我有一个 CustomObject 列表和一个模板文件 template.hbs。我可以通过使用句柄栏{{#each customList}}块来迭代这个列表。
现在我只想遍历我的 customList 中的索引对象。
服务器端 :
handlebars.registerHelper("even", new Helper<List<?>>() {
@Override
public Object apply(List<?> list, Options options)
throws IOException {
if(list!=null && list.size()>1) {
for(int index=0;index<list.size();index++) {
if((index+1) % 2 != 0) {
options.fn(list.remove(index));
}
}
}
return list;
Template.hbs 的一部分:
{{#even customList}}
{{customProperty1}}
{{/even}}
但这不会迭代我的 customList 而是将我的列表打印为字符串。