{{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.
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:
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:
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'}}
Type: String
Default value: \n
The separator to append after each inlined file.
Type: Function
Default value: function(a, b) {return a.index >= b.index ? 1 : -1;}
Compare function for sorting the aggregated files.
Options can be defined in either of the following ways:
Set options as hash arguments directly on the helper expressions themselves:
Note that Options defined in the hash always win!
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
}
}
}
See examples of the {{aggregate}}
helper being used in the yfm project:
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: {}
}
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:
And the files we want to aggregate include these Lo-Dash and Handlebars templates:
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>
Instead of writing out full paths, like this:
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:
Jon Schlinkert
Licensed under the MIT License Copyright (c) Jon Schlinkert, contributors.