An open API service indexing awesome lists of open source software.

https://github.com/jonschlinkert/handlebars-lint

Pass a context and a string with handlebars templates and lint for missing variables, helpers, block helpers or partials.
https://github.com/jonschlinkert/handlebars-lint

ast handlebars lint template templates

Last synced: about 1 month ago
JSON representation

Pass a context and a string with handlebars templates and lint for missing variables, helpers, block helpers or partials.

Awesome Lists containing this project

README

        

# handlebars-lint [![NPM version](https://img.shields.io/npm/v/handlebars-lint.svg?style=flat)](https://www.npmjs.com/package/handlebars-lint) [![NPM monthly downloads](https://img.shields.io/npm/dm/handlebars-lint.svg?style=flat)](https://npmjs.org/package/handlebars-lint) [![NPM total downloads](https://img.shields.io/npm/dt/handlebars-lint.svg?style=flat)](https://npmjs.org/package/handlebars-lint) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/handlebars-lint.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/handlebars-lint)

> Lint for missing variables, helpers, block helpers or partials.

Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm install --save handlebars-lint
```

## Usage

### [lint](index.js#L26)

Pass a context and a string with handlebars templates and lint for missing variables, helpers, block helpers or partials.

**Params**

* `str` **{String}**: The string to lint.
* `options` **{Object}**: Pass a context on `options.context` or your own instance of handlebars on `options.hbs`.
* `returns` **{Object}**

**Example**

```js
const lint = require('handlebars-lint');
lint(string, options);
```

## Example

Assuming we lint the following string:

```handlebars
Title: {{upper foo}}
Hi, my name is {{proper name}}
```

With the following context:

```js
const missing = lint(str, {
context: {
helpers: {
upper: function() {},
proper: function() {},
},
variables: {
name: 'Brian'
}
}
});
console.log(missing);
//=> { variables: [ 'foo' ] }
```

## Extended example

Assuming we lint the following string:

```handlebars
{{upper "bar"}}
{{upper varname}}
{{baz}}
{{zzz name=name}}

More content.

{{abc (sub blah)}}

{{> onetwo (three blah)}}
{{> four five}}

{{#markdown foo}}
> A markdown blockquote
{{> six (seven eight)}}
{{lower whatever}}
{{/markdown}}

The end.
```

The following:

```js
var missing = lint(str, {
context: {
helpers: {
upper: function() {}
},
variables: {
name: 'Brian'
}
}
});
console.log(missing);
```

Results in:

```js
{
helpers: [ 'upper', 'baz', 'zzz', 'abc', 'sub', 'three', 'seven', 'lower' ],
variables: [ 'varname', 'name', 'blah', 'five', 'eight', 'whatever', 'foo' ],
partials: [ 'onetwo', 'four', 'six' ],
blockHelpers: [ 'markdown' ]
}
```

**Heads up!**

The linter assumes that an expression is a variable when:

1. The expression has no params or hash arguments, as in `{{foo}}`, versus `{{foo bar}}` or `{{foo bar=baz}}`
2. There is no variable or helper on the context to distinguish otherwise.

## About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

```sh
$ npm install && npm test
```

Building docs

_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

To generate the readme, run the following command:

```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```

### Related projects

You might also be interested in these projects:

* [handlebars-helpers](https://www.npmjs.com/package/handlebars-helpers): More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… [more](https://github.com/helpers/handlebars-helpers) | [homepage](https://github.com/helpers/handlebars-helpers "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.")
* [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://github.com/jonschlinkert/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers "Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions.")

### Author

**Jon Schlinkert**

* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)

### License

Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 27, 2018._