helper-compose

{{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.

{{compose}}

{{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:

{{compose 'path/to/files/*.hbs'}}
  <h1>Title: {{title}}</h1>
  {{{content}}}</p>
{{/compose}}

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>{{title}}</h1>
<p class="heading">{{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 assemble task 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_fnfunction(ab) {
        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 -->',
      comparefunction(ab) {
        return a.index >= b.index ? 1 : -1;
      }
    }
  }
}

Instead of doing this:

{{compose 'path/to/my/blog/posts/*.hbs'}}
  <h1>{{post.title}}</h1>
  ...
{{/compose}}

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:

{{compose 'posts/*.hbs'}}
  <h1>{{post.title}}</h1>
  ...
{{/compose}}

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 -->
{{#compose 'posts/*.hbs' sep="<!-- post -->"}}
  <h1>{{blog.title}}</h1>
  <h2>Post title: {{title}}</h2>
  <p>{{{content}}}</p>
  <a href="{{relative link}}">{{text}}</a>
{{/compose}}

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

---
title: Monday
---
 
This is the {{title}} 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.