helpers/handlebars-utils

Name: handlebars-utils

Owner: Helpers

Description: Utils for handlebars helpers. Externalized from handlebars, to allow helpers to use the utils without having to depend on handlebars itself.

Created: 2017-01-25 18:46:39.0

Updated: 2017-10-16 10:32:26.0

Pushed: 2017-11-17 03:42:17.0

Homepage: null

Size: 28

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

handlebars-utils NPM version NPM monthly downloads NPM total downloads Linux Build Status

Utils for handlebars helpers. Externalized from handlebars, to allow helpers to use the utils without having to depend on handlebars itself.

Follow this project's author, Jon Schlinkert, for updates on this project and others.

(TOC generated by verb using markdown-toc)

Install

Install with npm:

m install --save handlebars-utils
Usage
utils = require('handlebars-utils');
API
.isBlock

Returns true if a helper is a block helper.

Params

Example

lebars.registerHelper('example', function(options) {
 (utils.isBlock(options)) {
// do something if this is a block helper
else {
// do something else if this is a not block helper


.fn

Returns the given value or renders the block if it's a block helper.

Params

Example

lebars.registerHelper('example', function(val, locals, options) {
turn utils.fn(val, locals, options);

.inverse

Returns the given value or renders the inverse block if it's a block helper.

Params

Example

lebars.registerHelper('example', function(val, locals, options) {
turn utils.inverse(val, locals, options);

.value

Gets the return value for a helper, by either rendering the block or inverse block if it's a block helper, or returning the given value (when truthy) or an empty string (when falsey) if it's a non-block expression.

Params

Example

lebars.registerHelper('example', function(val, locals, options) {
turn utils.value(val, locals, options);

.isOptions

Returns true if the given value is a handlebar options object.

Params

Example

lebars.registerHelper('example', function(val, locals, options) {
 (utils.isOptions(locals)) {
options = locals;
locals = {};

 do stuff

.isUndefined

Returns true if the given value is undefined or is a handlebars options hash (which means that a value was not passed by the user).

Params

Example

lebars.registerHelper('example', function(val, options) {
 (utils.isUndefined(val)) {
return '';

 do stuff

.isApp

Returns true if an app propery is on the context, which means the context was created by assemble, templates, verb, or any other library that follows this convention.

Params

Example

lebars.registerHelper('example', function(val, options) {
r context = options.hash;
 (utils.isApp(this)) {
context = Object.assign({}, this.context, context);

 do stuff

.options

Creates an options object from the context, locals and options. Handlebars' options.hash is merged onto the options, and if the context is created by templates, this.options will be merged onto the options as well.

Params

.context

Get the context to use for rendering.

Params

.isObject

Returns true if the given value is an object.

Params

Example

ole.log(utils.isObject(null));
 false
ole.log(utils.isObject([]));
 false
ole.log(utils.isObject(function() {}));
 false
ole.log(utils.isObject({}));
 true
.isEmpty

Returns true if the given value is “empty”.

Params

Example

ole.log(utils.isEmpty(0));
 false
ole.log(utils.isEmpty(''));
 true
ole.log(utils.isEmpty([]));
 true
ole.log(utils.isEmpty({}));
 true
.result

Returns the given value. If the value is a function it will be called with the current context, otherwise the value is returned.

Params

Example

ole.log(utils.result('foo'));
 'foo'
ole.log(utils.result(function() {
turn 'foo';

 'foo'
.identity

Returns the given value as-is, unchanged.

Params

Example

ole.log(utils.result('foo'));
 'foo'
ole.log(utils.result(function() {
turn 'foo';

 [function]
.isString

Return true if val is a non-empty string.

Params

.arrayify

Cast the given val to an array.

Params

Example

ole.log(utils.arrayify(''));
 []
ole.log(utils.arrayify('foo'));
 ['foo']
ole.log(utils.arrayify(['foo']));
 ['foo']
.tryParse

Try to parse the given string as JSON. Fails gracefully and always returns an object if the value cannot be parsed.

Params

About
Related projects

You might also be interested in these projects:

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

m 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:

m install && npm test
Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on September 04, 2017.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.