Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a separator between elements in an {{#each}} loop except after the last element?

I have a Handlebars template where I'm trying to generate a comma-separated list of items from an array.

In my Handlebars template:

{{#each list}}     {{name}} {{status}}, {{/each}} 

I want the , to not show up on the last item. Is there a way to do this in Handlebars or do I need to fall back to CSS selectors?

UPDATE: Based on Christopher's suggestion, this is what I ended up implementing:

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()     }) })); 

and in my view:

{{collection attachments}} 

and the item view:

{{content.title}} ({{content.size}}) {{#unless isLastItem}}, {{/unless}} 
like image 731
Chris Thompson Avatar asked May 01 '12 19:05

Chris Thompson


People also ask

How do you keep a gap between two elements in HTML?

Creating extra spaces before or after text To create extra spaces before, after, or in-between your text, use the   (non-breaking space) extended HTML character.

How do you add a separator in HTML?

The <hr> tag defines a thematic break in an HTML page (e.g. a shift of topic). The <hr> element is most often displayed as a horizontal rule that is used to separate content (or define a change) in an HTML page.

How do I add a space between elements in CSS?

The CSS padding properties are used to generate space around an element's content, inside of any defined borders. With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left).


1 Answers

I know I'm late to the parts but I found a WAYYYY simpler method

{{#unless @last}},{{/unless}} 
like image 54
MylesBorins Avatar answered Sep 25 '22 09:09

MylesBorins