https://github.com/helpers/helper-sort-items
Helper for filtering and sorting an assemble collection based on an array of strings.
https://github.com/helpers/helper-sort-items
assemble handlebars helper items sort
Last synced: 2 months ago
JSON representation
Helper for filtering and sorting an assemble collection based on an array of strings.
- Host: GitHub
- URL: https://github.com/helpers/helper-sort-items
- Owner: helpers
- License: mit
- Created: 2017-02-20T04:36:21.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-20T04:37:34.000Z (about 9 years ago)
- Last Synced: 2025-08-09T07:46:45.072Z (9 months ago)
- Topics: assemble, handlebars, helper, items, sort
- Language: JavaScript
- Homepage: https://github.com/helpers/helper-sort-items
- Size: 7.81 KB
- Stars: 2
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/contributing.md
- License: LICENSE
Awesome Lists containing this project
README
# helper-sort-items [](https://www.npmjs.com/package/helper-sort-items) [](https://npmjs.org/package/helper-sort-items) [](https://npmjs.org/package/helper-sort-items) [](https://travis-ci.org/helpers/helper-sort-items)
> Helper for filtering and sorting an assemble collection based on an array of strings.
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save helper-sort-items
```
## Usage
This is designed with [assemble](https://github.com/assemble/assemble) collections in mind, but it's generic enough that it should work with an array of strings too.
**Example**
The following example shows how to sort and filter the items in an [assemble](https://github.com/assemble/assemble) collection.
```js
var assemble = require('assemble');
var app = assemble();
app.helper('sortItems', require('helper-sort-items'));
app.pages('templates/*.hbs');
```
Given that four pages have been loaded: `foo.hbs`, `bar.hbs`, `baz.hbs`, and `some-file.hbs`, the following:
```handlebars
{{#pages}}
{{#each (sortItems items) as |item|}}
{{item.basename}}
{{/each}}
{{/pages}}
```
When rendered with these options:
```js
var locals = {order: ['baz', 'bar', 'foo']};
app.render('some-file.hbs', locals, function(err, view) {
if (err) {
console.log(err);
process.exit(1);
}
console.log(view.contents.toString());
});
```
Would result in:
```
baz.hbs
bar.hbs
foo.hbs
```
## Sort order
You can also pass the sort order as the last argument as a comma-separated string:
```handlebars
{{#pages}}
{{#each (sortItems items "baz,foo,bar") as |item|}}
{{item.basename}}
{{/each}}
{{/pages}}
```
Or when used as a [handlebars](http://www.handlebarsjs.com/) helper, you can pass the sort order on the options hash as a comma-separated string:
```handlebars
{{#pages}}
{{#each (sortItems items order="baz,foo,bar") as |item|}}
{{item.basename}}
{{/each}}
{{/pages}}
```
## About
### Related projects
* [handlebars-helpers](https://www.npmjs.com/package/handlebars-helpers): More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… [more](https://github.com/assemble/handlebars-helpers) | [homepage](https://github.com/assemble/handlebars-helpers "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.")
* [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://github.com/jonschlinkert/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers "Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions.")
### Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
### Building docs
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
To generate the readme, run the following command:
```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
### Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
```sh
$ npm install && npm test
```
### Author
**Jon Schlinkert**
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
### License
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
MIT
***
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 19, 2017._