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

v0.1.3

Published

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

5

Readme

{{aggregate}} NPM version

{{aggregate}} 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-aggregate --save-dev

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

grunt.initConfig({
  assemble: {
    options: {
      // the 'helper-aggregate' modules must also be listed in devDependencies
      // for assemble to automatically resolve the helper
      helpers: ['handlebars-helper-aggregate', 'other/helpers/*.js']
    }
    ...
  }
});

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

{{aggregate 'path/*.hbs'}}

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: <%= book.title %>
chapter: 1
heading: <%= book.title %> | Chapter <%= chapter %>
---
<h1>{{title}}</h1>
<p class="heading">{{heading}}</p>

Helper Options

cwd

This option is really only useful when options are defined in Assemble.

Type: String (optional) Default value: undefined

The current working directory, or "cwd", for paths defined in the helper. So instead of writing out {{aggregate 'my/book/chapters/*.hbs'}}, just define cwd: "my/book" and now any paths defined in the helper will use the cwd as a base, like this: {{aggregate 'chapters/*.hbs'}}

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

Options can be defined in either of the following ways:

hash options

Set options as hash arguments directly on the helper expressions themselves:

{{aggregate 'my/book/chapters/*.hbs' sep="<!-- Chapter -->"}}

Note that Options defined in the hash always win!

"assemble" task options

If you use Grunt and Assemble, you can pass options from the assemble task in the Gruntfile to the helper.

This helper registers the custom aggregate property, in the Assemble options, enabling options for the helper to be defined in the Assemble task or target options, e.g.:

assemble: {
  options: {
    aggregate: {
      // aggregate helper options here
    }
  }
}

Examples

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

example templates and content

example options and context

Example config with Assemble

In your project's Gruntfile, options for the {{aggregate}} helper can be defined in the Assemble task options:

assemble: {
  options: {
    helpers: ['handlebars-helper-aggregate'],
    aggregate: {
      cwd: 'path/to/files',
      sep: '<!-- separator defined in Gruntfile -->',
      compare: function (a, b) {
        return a.index >= b.index ? 1 : -1;
      }
    }
  },
  files: {}
}

Usage example

Given you have this config in your project's gruntfile:

// Project configuration.
grunt.initConfig({

  // Metadata for our book.
  book: require('./metadata/book.yml'),

  assemble: {
    options: {
      helpers: ['handlebars-helper-aggregate'],
      aggregate: {
        sep: '<!-- chapter -->'
      },
      book: {
        src: ['chapters.hbs'],
        dest: 'book/'
      }
    }
  }
});

Our chapters.hbs file contains the following:

{{{aggregate 'chapters/*.hbs'}}}

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

---
title: <%= book.title %>
chapter: 1
intro: Chapter <%= chapter %>
---
<h1>Content from {{title}}</h1>
<p class="intro">{{intro}}</p>
<p class="chapter">Chapter: {{chapter}}</p>

The result, book/chapters.html would contain something like:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>My Amazing Book</title>
  </head>
  <body>

    <!-- chapter -->
    <h1>Content from My Amazing Book</h1>
    <p class="intro">Chapter 1</p>
    <p class="chapter">Chapter: 1</p>

    <!-- chapter -->
    <h1>Content from My Amazing Book</h1>
    <p class="intro">Chapter 2</p>
    <p class="chapter">Chapter: 2</p>

    <!-- chapter -->
    <h1>Content from My Amazing Book</h1>
    <p class="intro">Chapter 3</p>
    <p class="chapter">Chapter: 3</p>
  </body>
</html>

cwd example

Instead of writing out full paths, like this:

{{aggregate 'my/book/chapters/*.hbs'}}
{{aggregate 'my/book/extras/*.hbs'}}

Just define a cwd in the aggregate options in your project's Gruntfile:

assemble: {
  options: {
    helpers: ['handlebars-helper-aggregate'],
    aggregate: {
      cwd: 'my/book' // "base" path to prepend
    }
  }
}

Now you can define paths in the templates like this:

{{aggregate 'chapters/*.hbs'}}
{{aggregate 'extras/*.hbs'}}

Author

Jon Schlinkert

License and Copyright

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