useful-handlebars-helpers
Version:
More than 150 Handlebars helpers in 17 categories. Works in browsers and in Node.js.
73 lines (63 loc) • 1.49 kB
JavaScript
;
const getValue = require('get-value');
const util = require('handlebars-utils');
const typeOf = require('kind-of');
const helpers = module.exports;
/**
* Return the given value of `prop` from `this.options`.
*
* ```handlebars
* <!-- context = {options: {a: {b: {c: 'ddd'}}}} -->
* {{option "a.b.c"}}
* <!-- results => `ddd` -->
* ```
* @param {String} `prop`
* @return {any}
* @api public
*/
helpers.option = function(prop, locals, options) {
return getValue(util.options(this, locals, options), prop);
};
/**
* Block helper that renders the block without taking any arguments.
*
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.noop = function(options) {
return options.fn(this);
};
/**
* Get the native type of the given `value`
*
* ```handlebars
* {{typeOf 1}}
* //=> 'number'
* {{typeOf "1"}}
* //=> 'string'
* {{typeOf "foo"}}
* //=> 'string'
* ```
* @param {any} `value`
* @return {String} Returns the type of value.
* @api public
*/
helpers.typeOf = typeOf;
/**
* Block helper that builds the context for the block
* from the options hash.
*
* @param {Object} `options` Handlebars provided options object.
* @contributor Vladimir Kuznetsov <https://github.com/mistakster>
* @block
* @api public
*/
helpers.withHash = function(options) {
if (options.hash && Object.keys(options.hash).length) {
return options.fn(options.hash);
} else {
return options.inverse(this);
}
};