Giter Site home page Giter Site logo

handlebars-group-by's Introduction

handlebars-group-by

Handlebars helper which allows you to group lists by a property of each item.

NPM version Downloads Build Status Coverage Status

Install

With Node.js:

$ npm install handlebars-group-by

With Bower:

$ bower install shannonmoeller/handlebars-group-by

Helper

{{#group [list] by="[property]"}}

  • name 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"}}
	<h1>{{ value }}</h1>

	{{#each items}}
		<h2>{{ title }}</h2>
		<p>{{ body }}</p>

	{{/each}}
{{/group}}

Output:

<h1>2014</h1>

<h2>foo</h2>
<p>foo bar</p>

<h2>bar</h2>
<p>bar baz</p>

<h2>baz</h2>
<p>baz bat</p>

<h1>2015</h1>

<h2>bat</h2>
<p>bat qux</p>

<h2>qux</h2>
<p>qux foo</p>

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 = require('handlebars'),
    groupBy = require('handlebars-group-by');

groupBy(handlebars);

groupBy.register(handlebars)

  • handlebars Handlebars - An instance of Handlebars.

Helpers are also exposed via a register method for use with Assemble.

var handlebars = require('handlebars'),
    groupBy = require('handlebars-group-by');

groupBy.register(handlebars);

// or

grunt.initConfig({
    assemble: {
        options: {
            helpers: ['path/to/handlebars-group-by.js']
        }
    }
});

Contribute

Tasks Chat Tip

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.