Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 1.4 KB

File metadata and controls

70 lines (53 loc) · 1.4 KB

Usage

This is designed with [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][] collection.

var assemble = require('assemble');
var app = assemble();

app.helper('sortItems', require('{%= name %}'));
app.pages('templates/*.hbs');

Given that four pages have been loaded: foo.hbs, bar.hbs, baz.hbs, and some-file.hbs, the following:

<!-- in "some-file.hbs" -->
{{#pages}}
  {{#each (sortItems items) as |item|}}
    {{item.basename}}
  {{/each}}
{{/pages}}

When rendered with these options:

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:

{{#pages}}
  {{#each (sortItems items "baz,foo,bar") as |item|}}
    {{item.basename}}
  {{/each}}
{{/pages}}

Or when used as a [handlebars][] helper, you can pass the sort order on the options hash as a comma-separated string:

{{#pages}}
  {{#each (sortItems items order="baz,foo,bar") as |item|}}
    {{item.basename}}
  {{/each}}
{{/pages}}