# broccoli-handlebars

> Compile handlebars templates with helpers and dynamic contexts

Latest version **1.0.1** (published 2018-06-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install broccoli-handlebars
pnpm add broccoli-handlebars
yarn add broccoli-handlebars
bun add broccoli-handlebars
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-06-11 |
| First published | 2014-07-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.9.2 |
| Dependencies | 6 |
| Unpacked size | 12.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Moudy |
| Maintainers | moudy |
| Keywords | broccoli-plugin, handlebars |

## Links

- npm: https://www.npmjs.com/package/broccoli-handlebars
- Repository: https://github.com/moudy/broccoli-handlebars
- Homepage: https://github.com/moudy/broccoli-handlebars#readme
- Issues: https://github.com/moudy/broccoli-handlebars/issues
- npm.io page: https://npm.io/package/broccoli-handlebars

## Dependencies (6)

- [rsvp](https://npm.io/package/rsvp.md) ^3.0.9
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.0
- [walk-sync](https://npm.io/package/walk-sync.md) ^0.3.0
- [handlebars](https://npm.io/package/handlebars.md) ^4.0.6
- [broccoli-plugin](https://npm.io/package/broccoli-plugin.md) ^1.3.0
- [broccoli-kitchen-sink-helpers](https://npm.io/package/broccoli-kitchen-sink-helpers.md) ^0.3.1

## Recent versions

- 1.0.1 (latest) — 2018-06-11
- 1.0.0 — 2017-01-06
- 0.0.6 — 2014-11-28
- 0.0.5 — 2014-11-20
- 0.0.3 — 2014-07-26
- 0.0.2 — 2014-07-17
- 0.0.1 — 2014-07-13
- 0.0.0 — 2014-07-12

## README

# Broccoli Handlebars

[Broccoli](https://github.com/broccolijs/broccoli) plugin for compiling handlebars templates that supports using an existing Handlebars instance, partials, helpers, and asynchronous render context based on the filename.

### Install
```
npm install --save broccoli-handlebars
```

### Example
```js
var Handlebars = require('handlebars');
var broccoliHandlebars = require('broccoli-handlebars');
var helpers = require('./my-helpers-object');
var dataForFile = require('./get-view-data');

var tree = 'site';

var tree = broccoliHandlebars(tree, ['pages/**/*.hbs'], {
  helpers: helpers
, handlebars: Handlebars
, partials: 'partials-path'
, context: function (filename) { return dataForFile(filename); }
, destFile: function (filename) { return filename.replace(/\.(hbs|handlebars)$/, ''); }
});
```

### Usage

```js
var hbsTree = broccoliHandlebars(tree, [outputFiles], options);
```
- **tree** - a broccoli tree or string of handlebars files to watch (including partials for example)
- **outputFiles** - an array of filenames or globs that will be compiled
- **options** - Handlebars options, see below


### Options

#### context (optional)
A function or object used as the render context. The function is passed the filename allowing for dynamic contexts. The function may return a value directly or a promise the resolves to a value.
```js
function RenderContext () {}

RenderContext.prototype.render = function (filename) {
  return {
    title: filename.toUpperCase()
  };
};

var renderContext = new RenderContext();

var tree = broccoliHandlebars(tree, {
  // An object that is the same for each file
  context: { title: 'Foo' }

  // or renter data based on the file name
  context: renderContext.render.bind(renderContext)

  // or return a promise
  context: function () { return $.getJSON('/data.json'); }
});
```

#### destFile (optional)
A function that returns the name of a Handlebars-compiled file in the Broccoli output tree. The function is called for every input file, with `filename` supplied. If no function is supplied, the default is for the .hbs or .handlebars suffix of `filename` to be replaced with .html. E.g. `example.hbs` becomes `example.html`.

```js
var tree = broccoliHandlebars(tree, {
    destFile: function (filename) { return filename.replace(/\.(hbs|handlebars)$/, ''); }
});

// Generate output files like Rails sprockets
// example.json.hbs -> example.json 
```


#### helpers (optional)
An object of helpers or a function that returns an object of helpers.
```js
module.exports = {
  firstName: function (str) { return str.split(' ')[0]; }
};

// Hi {{firstName user.fullName}}
```

#### handelbars (optional)
A Handlebars instance. Useful if you need to make sure you are using a specific version or have already registerd partials/helpers.
```js
var tree = broccoliHandlebars(tree, {
  handlebars: require('handlebars')
});
```

#### partials (optional)
A string that is the path to partials.
```js
var tree = broccoliHandlebars(tree, {
  partials: 'path/to/partials'
});
```

---
_Source: https://npm.io/package/broccoli-handlebars · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
