
{{compose}} 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 helper-compose --save-dev
Next, in your Gruntfile, simply add helper-compose to the helpers property in the Assemble task or target options:
grunt.initConfig({
assemble: {
options: {
// the 'helper-compose' modules must also be listed in devDependencies
// for assemble to automatically resolve the helper
helpers: ['helper-compose']
}
files: {...}
}
});
With that completed, you may now use the {{compose}} helper in your templates:
<h1>Title: </h1>
</p>
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: <%= blog.title %>
post: 1
heading: <%= blog.title %> | Blog <%= post %>
---
<h1></h1>
<p class="heading"></p>
Type: String (optional)
Default value: ''
The cwd for paths defined in the helper.
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.
If you use Grunt and Assemble, you can pass options from the
assembletask in the Gruntfile to the helper.
In your project's Gruntfile, options for the {{#compose}}...{{/compose}} helper can be defined in the Assemble task options:
assemble: {
options: {
helpers: ['helper-compose', 'other/helpers/*.js'],
compose: {
cwd: 'test/fixtures/includes',
sep: '<!-- include -->',
compare_fn: function(a, b) {
return a.index >= b.index ? 1 : -1;
}
}
},
files: {}
}
Note that the options are defined in options: {compose: {}}, which is a custom property in the Assemble options.
See examples of the {{compose}} helper being used in the yfm project:
assemble: {
options: {
compose: {
cwd: 'test/fixtures/compose',
sep: '<!-- include -->',
compare: function(a, b) {
return a.index >= b.index ? 1 : -1;
}
}
}
}
Instead of doing this:
<h1></h1>
...
You could define the cwd in the compose options in your project's Gruntfile:
assemble: {
options: {
helpers: ['helper-compose'],
compose: {
cwd: 'path/to/my/blog'
}
}
}
and then define paths in the templates like this:
<h1></h1>
...
Given you have this config in your project's gruntfile:
// Project configuration.
grunt.initConfig({
// Metadata for our blog.
blog: require('./test/fixtures/blog/blog.yml'),
assemble: {
options: {
helpers: ['helper-compose'],
compose: {
cwd: 'blog',
sep: '<!-- post -->'
}
},
blog: {
src: ['index.hbs'],
dest: 'blog/'
}
}
});
Our index.hbs file contains the following:
<!-- post -->
<h1></h1>
<h2>Post title: </h2>
<p></p>
<a href=""></a>
And the files we want to compose include these Lo-Dash and Handlebars templates:
---
title: Monday
---
This is the post...
The result, blog/index.html would contain something like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Brilliant Blog</title>
</head>
<body>
<!-- post -->
<h1>My Brilliant Blog</h1>
<h2>Post title: Monday</h2>
<p>This is the Monday post...</p>
<!-- post -->
<h1>My Brilliant Blog</h1>
<h2>Post title: Tuesday</h2>
<p>This is the Tuesday post...</p>
<!-- post -->
<h1>My Brilliant Blog</h1>
<h2>Post title: Wednesday</h2>
<p>This is the Wednesday post...</p>
</body>
</html>
Jon Schlinkert
Licensed under the MIT License. Copyright (c) Jon Schlinkert, contributors.