我有一个Handlebars模板,试图从数组中生成一个用逗号分隔的项目列表。
在我的车把模板中:
{{#each list}}
{{name}} {{status}},
{{/each}}
我希望,
不会出现在最后一项上。有没有办法在Handlebars中做到这一点,或者我需要回退到CSS选择器吗?
更新:根据克里斯托弗的建议,这就是我最终实现的目标:
var attachments = Ember.CollectionView.extend({
content: [],
itemViewClass: Ember.View.extend({
templateName: 'attachments',
tagName: 'span',
isLastItem: function() {
return this.getPath('parentView.content.lastObject') == this.get('content');
}.property('parentView.content.lastObject').cacheable()
})
}));
在我看来:
{{collection attachments}}
和项目视图:
{{content.title}} ({{content.size}}) {{#unless isLastItem}}, {{/unless}}
您可以使用标准CSS来执行此操作:
li:after {
content: ',';
}
li:last-of-type:after {
content: '';
}
我更喜欢单独的规则,但是如果可读性稍差一些,则要更简洁(来自注释中的@Jay):
li:not(:last-of-type):after {
content: ',';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句