helpers /  Compose

Compose

{{compose}} NPM version

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

Quickstart

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 }} 

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: <%= blog.title %> 
post: 1
heading: <%= blog.title %>  | Blog <%= post %> 
---
<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

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

Examples

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

example templates and content

example options and context

all options

assemble: {
  options: {
    compose: {
      cwd: 'test/fixtures/compose',
      sep: '<!-- include -->',
      compare: function (a, b)  { 
        return  a.index >= b.index ? 1  : -1;
      }
    }
  }
}

cwd option

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 }} 

Usage example

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 '}} 
  <h1 > {{blog.title }} </h1 > 
  <h2 > Post title: {{title }} </h2 > 
  <p > Post {{{content }} }</p > 
{{/compose }} 

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

---
title: Alpha post
---
This is the {{title }}, which should be inserted in the composed result.

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: Alpha</h2>
    <p>Post This is the Alpha post, which should be inserted in the composed result. </p>
    <!-- post -->
    <h1>My Brilliant Blog</h1>
    <h2>Post title: Beta</h2>
    <p>Post This is the Beta post, which should be inserted in the composed result. </p>
    <!-- post -->
    <h1>My Brilliant Blog</h1>
    <h2>Post title: Gamma</h2>
    <p>Post This is the Gamma post, which should be inserted in the composed result. </p>
  </body>
</html>

Author

Jon Schlinkert

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


See the template for this page →

Find an error? Let us know →