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

helper-compose

v0.1.1

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.

Downloads

0

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

Author

Jon Schlinkert

License and Copyright

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