npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

handlebars-helper-compose

v0.2.12

Published

{{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. Good for blog posts, chapters, etc.

Downloads

244

Readme

{{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 handlebars-helper-compose --save-dev

Next, in your Gruntfile, simply add handlebars-helper-compose to the helpers property in the Assemble task or target options:

grunt.initConfig({
  assemble: {
    options: {
      // the 'handlebars-helper-compose' modules must also be listed in devDependencies
      // for assemble to automatically resolve the helper
      helpers: ['handlebars-helper-compose']
    }
    files: {
      '_gh_pages/': ['templates/*.hbs']
    }
  }
});

With that completed, you may now use the {{compose}} helper in your templates:

{{compose src="blog/posts/*.md"}}
  <h1>Title: {{@title}}</h1>
  {{{@content}}}</p>
{{/compose}}

Note that the path used in the src hash option is relative to the project root (e.g. Gruntfile).

Context & Lo-Dash templates

The helper will also process any valid Lo-Dash templates in the YAML front matter of any targeted files. For example:

---
title: <%= blog.title %>
post: 1
heading: <%= blog.title %> | Blog <%= post %>
---
<h1>{{title}}</h1>
<p class="heading">{{heading}}</p>

Options

src

Type: String (optional)

Default: undefined

The file path of the file(s) to include. Glob patterns may be used.

cwd

Type: String (optional)

Default: undefined

The cwd for paths defined in the helper.

sep

Type: String

Default: \n

The separator to append after each inlined file.

marked

Type: Object

Default: \n

The separator to append after each inlined file.

filter

Type: function

Default: undefined

A custom function for filtering the array of paths returned from the src property. This could potentially be more flexible, please make a feature request if you have a use case.

compare

Type: Function

Default: 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: ['handlebars-helper-compose', 'foo/*.js'],
    compose: {
      cwd: './posts',
      sep: '<!-- post -->',
      compare: function(a, b) {
        return a.index >= b.index ? 1 : -1;
      }
    }
  },
  files: {}
}

Note that the options are defined in options: {compose: {}}, which is registered by this helper as a custom property in the Assemble options.

Examples

all options

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

filtering

Example: return only the last two items from the src files array:

assemble: {
  options: {
    compose: {
      cwd: 'posts',
      sep: '<!-- post -->',
      filter: function(arr) {
        return arr.slice(Math.max(arr.length - 2, 0));
      }
    }
  }
}

cwd option

Instead of defining the entire path in the src hash option, like this:

{{compose src="path/to/my/blog/posts/*.md"}}
  <h1>{{@title}}</h1>
  {{@content}}
{{/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/*.md'}}
  <h1>{{@title}}</h1>
  {{@content}}
{{/compose}}

Usage example

In our Gruntfile, let's say we have the following config:

// Project configuration.
grunt.initConfig({

  // Metadata for our blog.
  blog: require('./test/fixtures/blog/blog.yml'),
  assemble: {
    options: {
      helpers: ['handlebars-helper-compose'],
      compose: {
        cwd: 'blog',
        sep: '<!-- post -->'
      }
    },
    blog: {
      src: ['index.hbs'],
      dest: 'blog/'
    }
  }
});

page

...and index.hbs file contains the following:

<!-- post -->
{{#compose src="posts/*.md" sep="<!-- post -->"}}
  <h1>{{blog.title}}</h1>
  <h2>Post title: {{@title}}</h2>
  <p>{{{@content}}}</p>
{{/compose}}

posts

..and we have a few posts, monday.md, tuesday.md, and wednesday.md, for example:

---
title: Monday
---

This is the {{title}} post...

result

The result, blog/index.html would look 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>

Author

Jon Schlinkert

License and Copyright

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