{{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 helper-aggregate --save-dev
In your Gruntfile, simply add helper-aggregate
to the helpers
property in the Assemble task or target options:
grunt;
With that completed, you may now use the {{aggregate}}
helper in your templates:
{{aggregate 'path/to/*.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: 1heading: <%= book.title %> | Chapter <%= chapter %>---<h1>{{title}}</h1><p class="heading">{{heading}}</p>
Options
cwd
Type: String
(optional)
Default value: ''
The cwd
for paths defined in the helper.
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
hash options
Set options as hash arguments directly on the expressions themselves:
{{aggregate 'my/book/chapters/*.hbs' sep="<!- Chapter -->"}}
If defined, 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.
In your project's Gruntfile, options for the {{aggregate}}
helper can be defined in the Assemble task options:
assemble: options: helpers: 'helper-aggregate' 'other/helpers/*.js' aggregate: cwd: 'path/to/files' sep: '<!-- separator defined in Gruntfile -->' { return aindex >= bindex ? 1 : -1; } files: {}
Note that the options are defined in options: {aggregate: {}}
, which is a custom property in the Assemble options.
Examples
See examples of the {{aggregate}}
helper being used in the yfm project:
example templates and content
example options and context
cwd example
Instead of doing this:
{{aggregate 'my/book/chapters/*.hbs'}}{{aggregate 'my/book/extras/*.hbs'}}
You could define the cwd
in the aggregate
options in your project's Gruntfile:
assemble: options: helpers: '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'}}
Usage example
Given you have this config in your project's gruntfile:
// Project configuration.grunt;
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: 1intro: 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:
My Amazing Book <!-- chapter --> Content from My Amazing Book Chapter 1 Chapter: 1 <!-- chapter --> Content from My Amazing Book Chapter 2 Chapter: 2 <!-- chapter --> Content from My Amazing Book Chapter 3 Chapter: 3
Author
Jon Schlinkert
License and Copyright
Licensed under the MIT License Copyright (c) Jon Schlinkert, contributors.