Using Handlebarjs, I’d like to loop over an array, and display values, separated by a separator. It would be easy if the content I want to display wasn’t a template too 😉
Here’s my case :
accounts = [
{'name': 'John', 'email': 'john@example.com'},
{'name': 'Malcolm', 'email': 'malcolm@example.com'},
{'name': 'David', 'email': 'david@example.com'}
];
{{#each accounts}}
<a href="mailto:{{ email }}" title="Send an email to {{ name }}">{{ name }}</a>,
{{/each}}
The problem with this implementation is that I will have this output :
John, Malcolm, David,
And I’d like it to be :
John, Malcolm, David
How can I do that ?
You can use CSS pseudo class
:after, together with content, to achieve the required “formatting”.(
:afterand content support in most browser today, and IE8+.)For example:
HTML:
CSS:
Result: