handlebars-group-by
Handlebars helper which allows you to group lists by a property of each item.
Install
With Node.js:
$ npm install handlebars-group-by
With Bower:
$ bower install shannonmoeller/handlebars-group-by
Helper
{{#group [list] by=[property]}}
list
Array
- Array whose items should be grouped together.by
String
- The name of the property by whose value items should be grouped.
Data:
posts: year: 2014 title: 'foo' body: 'foo bar' year: 2014 title: 'bar' body: 'bar baz' year: 2014 title: 'baz' body: 'baz bat' year: 2015 title: 'bat' body: 'bat qux' year: 2015 title: 'qux' body: 'qux foo'
Template:
{{#group posts by="year"}} {{ value }} {{#each items}} {{ title }} {{ body }} {{/each}}{{/group}}
Output:
2014 foofoo bar barbar baz bazbaz bat 2015 batbat qux quxqux foo
Api
Helpers are registered by passing in your instance of Handlebars. This allows you to selectively register the helpers on various instances of Handlebars.
groupBy(handlebars)
handlebars
Handlebars
- An instance of Handlebars.
var handlebars = groupBy = ; ;
groupBy.register(handlebars)
handlebars
Handlebars
- An instance of Handlebars.
Helpers are also exposed via a register
method for use with Assemble.
var handlebars = groupBy = ; groupBy; // or grunt;
Contribute
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.
Test
$ gulp test
License
MIT