UNPKG

helper-cache

Version:

Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers.

248 lines (159 loc) 6.93 kB
# helper-cache [![NPM version](https://img.shields.io/npm/v/helper-cache.svg?style=flat)](https://www.npmjs.com/package/helper-cache) [![NPM monthly downloads](https://img.shields.io/npm/dm/helper-cache.svg?style=flat)](https://npmjs.org/package/helper-cache) [![NPM total downloads](https://img.shields.io/npm/dt/helper-cache.svg?style=flat)](https://npmjs.org/package/helper-cache) [![Linux Build Status](https://img.shields.io/travis/helpers/helper-cache.svg?style=flat&label=Travis)](https://travis-ci.org/helpers/helper-cache) > Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers. ## Table of Contents - [Install](#install) - [API](#api) - [About](#about) _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_ ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save helper-cache ``` Install with [yarn](https://yarnpkg.com): ```sh $ yarn add helper-cache ``` ## API ### [HelperCache](index.js#L19) Create an instance of `HelperCache` with the given `options.` **Params** * `options` **{Object}** **Example** ```js var App = require('helper-cache'); var app = new App(); ``` ### [.addHelper](index.js#L47) Register a helper. **Params** * `name` **{String}** * `fn` **{Function}** * `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining. **Example** ```js app.helper('uppercase', function(str) { return str.toUpperCase(); }); ``` ### [.getHelper](index.js#L66) Get a helper. **Params** * `name` **{String}** * `fn` **{Function}** * `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining. **Example** ```js app.helper('uppercase', function(str) { return str.toUpperCase(); }); ``` ### [.helper](index.js#L84) Register a sync template helper `fn` as `name`. **Params** * `name` **{String}** * `fn` **{Function}** * `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining. **Example** ```js app.helper('uppercase', function(str) { return str.toUpperCase(); }); ``` ### [.helpers](index.js#L116) Register multiple sync helpers at once. **Params** * `helpers` **{Object}**: Array of globs, file paths or key-value pair helper objects. * `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining. **Example** ```js app.helpers({ foo: function() {}, bar: function() {}, baz: function() {} }); ``` ### [.asyncHelper](index.js#L135) Register an async template helper `fn` as `name`. **Params** * `name` **{String}** * `fn` **{Function}** * `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining. **Example** ```js app.asyncHelper('uppercase', function(str) { return str.toUpperCase(); }); ``` ### [.asyncHelpers](index.js#L164) Register multiple async helpers at once. **Params** * `helpers` **{Object}**: Array of globs, file paths or key-value pair helper objects. * `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining. **Example** ```js app.asyncHelpers({ foo: function() {}, bar: function() {}, baz: function() {} }); ``` ### [.group](index.js#L179) Namespace a collection of sync helpers on the given `prop`. **Params** * `helpers` **{Object|Array}**: Object, array of objects, or glob patterns. **Example** ```js app.group('mdu', require('markdown-utils')); // Usage: '<%= mdu.heading("My heading") %>' ``` ### [.asyncGroup](index.js#L202) Namespace a collection of async helpers on the given `prop`. **Params** * `helpers` **{Object|Array}**: Object, array of objects, or glob patterns. **Example** ```js app.asyncGroup('mdu', require('markdown-utils')); // Usage: '<%= mdu.heading("My heading") %>' ``` ### [.load](index.js#L234) Load helpers. **Params** * `helpers` **{Object}**: Array of globs, file paths or key-value pair helper objects. * `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining. **Example** ```js app.load({ foo: function() {}, bar: function() {}, baz: function() {} }); ``` ## About ### Related projects * [engine-cache](https://www.npmjs.com/package/engine-cache): express.js inspired template-engine manager. | [homepage](https://github.com/jonschlinkert/engine-cache "express.js inspired template-engine manager.") * [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.") * [template](https://www.npmjs.com/package/template): Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template… [more](https://github.com/jonschlinkert/template) | [homepage](https://github.com/jonschlinkert/template "Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template helpers, middleware, routes, loaders, and lots more. Powers assemble, verb and other node.js apps.") ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). ### 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 ``` ### 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 ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [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.5.0, on April 20, 2017._