我正在创建一张幻灯片 – 所以每个div都有3张图像
<div> <img /> <img /> <img /> </div> </div> <img /> <img /> <img /> </div>
互联网上的代码都没有完美无缺 –
http://jaketrent.com/post/every-nth-item-in-handlebars-loop/
http://rockycode.com/blog/handlebars-loop-index/
http://www.arlocarreon.com/blog/javascript/handlebars-js-everyother-helper/
是的,包括堆栈溢出中的答案.
任何人都可以提供一些在当前时期(Ember / Handlebar版本)完美运行的代码吗?
我有一系列模型,所以我想做类似的事情
{{#each model}} {{if index % 3 == 0}} {{/if}} {{/each}}
解决方法
我发现索引或@index在模板中不起作用,但您可以从帮助程序中访问它.
我在这里做了一个例子来证明这一点:
http://jsbin.com/egoyay/1/edit
编辑:添加要回答的代码,演示{{else}}阻止
把手帮手(非Ember使用):
Handlebars.registerHelper('ifIsNthItem',function(options) { var index = options.data.index + 1,nth = options.hash.nth; if (index % nth === 0) return options.fn(this); else return options.inverse(this); });
用法:
<ol> {{#each model}} <li> {{#ifIsNthItem nth=3}} Index is a multiple of 3 {{else}} Index is NOT a multiple of 3 {{/ifIsNthItem}} </li> {{/each}} </ol>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。