handlebars-helper-aggregate

{{aggregate}} handlebars helper. Inlines content from multiple files optionally using wildcard (globbing/minimatch) patterns, extracts YAML front matter to pass to context for each file. Accepts compare function as 3rd parameter for sorting inlined files.

npm install handlebars-helper-aggregate
1 downloads in the last day
4 downloads in the last week
11 downloads in the last month

{{aggregate}} NPM version

{{aggregate}} handlebars helper. Inlines content from multiple files optionally using wildcard (globbing/minimatch) patterns, extracts YAML front matter to pass to context for each file. Accepts compare function as 3rd parameter for sorting inlined files.

Quickstart

In the root of your project, run the following in the command line:

npm i handlebars-helper-aggregate --save-dev

In your Gruntfile, simply add handlebars-helper-aggregate to the helpers property in the Assemble task or target options:

grunt.initConfig({
  assemble: {
    options: {
      // the 'helper-aggregate' modules must also be listed in devDependencies
      // for assemble to automatically resolve the helper
      helpers: ['handlebars-helper-aggregate', 'other/helpers/*.js']
    }
    ...
  }
});

With that completed, you may now use the {{aggregate}} helper in your templates:

{{aggregate 'path/*.hbs'}}

Context & Lo-Dash templates

The helper will also process any valid Lo-Dash templates in the YAML front matter of targeted files, using grunt.config.data and the context of the "current" file. For example:

---
title: <%= book.title %>
chapter: 1
heading: <%= book.title %> | Chapter <%= chapter %>
---
<h1>{{title}}</h1>
<p class="heading">{{heading}}</p>

Helper Options

cwd

This option is really only useful when options are defined in Assemble.

Type: String (optional) Default value: undefined

The current working directory, or "cwd", for paths defined in the helper. So instead of writing out {{aggregate 'my/book/chapters/*.hbs'}}, just define cwd: "my/book" and now any paths defined in the helper will use the cwd as a base, like this: {{aggregate 'chapters/*.hbs'}}

sep

Type: String Default value: \n

The separator to append after each inlined file.

compare

Type: Function Default value: function(a, b) {return a.index >= b.index ? 1 : -1;}

Compare function for sorting the aggregated files.

Defining options

Options can be defined in either of the following ways:

hash options

Set options as hash arguments directly on the helper expressions themselves:

{{aggregate 'my/book/chapters/*.hbs' sep="<!-- Chapter -->"}}

Note that Options defined in the hash always win!

"assemble" task options

If you use Grunt and Assemble, you can pass options from the assemble task in the Gruntfile to the helper.

This helper registers the custom aggregate property, in the Assemble options, enabling options for the helper to be defined in the Assemble task or target options, e.g.:

assemble: {
  options: {
    aggregate: {
      // aggregate helper options here
    }
  }
}

Examples

See examples of the {{aggregate}} helper being used in the yfm project:

example templates and content

example options and context

Example config with Assemble

In your project's Gruntfile, options for the {{aggregate}} helper can be defined in the Assemble task options:

assemble: {
  options: {
    helpers: ['handlebars-helper-aggregate'],
    aggregate: {
      cwd: 'path/to/files',
      sep: '<!-- separator defined in Gruntfile -->',
      compare: function (a, b) {
        return a.index >= b.index ? 1 : -1;
      }
    }
  },
  files: {}
}

Usage example

Given you have this config in your project's gruntfile:

// Project configuration.
grunt.initConfig({

  // Metadata for our book.
  book: require('./metadata/book.yml'),

  assemble: {
    options: {
      helpers: ['handlebars-helper-aggregate'],
      aggregate: {
        sep: '<!-- chapter -->'
      },
      book: {
        src: ['chapters.hbs'],
        dest: 'book/'
      }
    }
  }
});

Our chapters.hbs file contains the following:

{{{aggregate 'chapters/*.hbs'}}}

And the files we want to aggregate include these Lo-Dash and Handlebars templates:

---
title: <%= book.title %>
chapter: 1
intro: Chapter <%= chapter %>
---
<h1>Content from {{title}}</h1>
<p class="intro">{{intro}}</p>
<p class="chapter">Chapter: {{chapter}}</p>

The result, book/chapters.html would contain something like:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>My Amazing Book</title>
  </head>
  <body>

    <!-- chapter -->
    <h1>Content from My Amazing Book</h1>
    <p class="intro">Chapter 1</p>
    <p class="chapter">Chapter: 1</p>

    <!-- chapter -->
    <h1>Content from My Amazing Book</h1>
    <p class="intro">Chapter 2</p>
    <p class="chapter">Chapter: 2</p>

    <!-- chapter -->
    <h1>Content from My Amazing Book</h1>
    <p class="intro">Chapter 3</p>
    <p class="chapter">Chapter: 3</p>
  </body>
</html>

cwd example

Instead of writing out full paths, like this:

{{aggregate 'my/book/chapters/*.hbs'}}
{{aggregate 'my/book/extras/*.hbs'}}

Just define a cwd in the aggregate options in your project's Gruntfile:

assemble: {
  options: {
    helpers: ['handlebars-helper-aggregate'],
    aggregate: {
      cwd: 'my/book' // "base" path to prepend
    }
  }
}

Now you can define paths in the templates like this:

{{aggregate 'chapters/*.hbs'}}
{{aggregate 'extras/*.hbs'}}

Author

Jon Schlinkert

Licensed under the MIT License Copyright (c) Jon Schlinkert, contributors.

npm loves you