Giter Site home page Giter Site logo

andersdjohnson / compose Goto Github PK

View Code? Open in Web Editor NEW

This project forked from helpers/handlebars-helper-compose

0.0 2.0 0.0 88 KB

{{compose}} handlebars helper. Similar to {{aggregate}}, but this is a block expression helper that inlines content from multiple files differently, extracting YAML front matter to pass to context for each file. Optionally use wildcard (globbing/minimatch) patterns. Accepts compare function as 3rd parameter for sorting inlined files.

License: MIT License

JavaScript 100.00%

compose's Introduction

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

License and Copyright

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

compose's People

Contributors

jonschlinkert avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.