130+ handlebars helpers in ~20 categories. Can be used with Assemble, Ghost, YUI, express.js etc.
JavaScript
Latest commit 8aa4382 Aug 17, 2016 @jonschlinkert jonschlinkert 0.7.5

README.md

handlebars-helpers NPM version NPM downloads Build Status

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.

You might also be interested in template-helpers.

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install --save handlebars-helpers

Browser usage

See how to use handlebars-helpers in the browser.

Usage

The main export returns a function that needs to be called to expose the object of helpers.

var helpers = require('handlebars-helpers')();
//=> returns object with all (130+) helpers

Get a specific collection

Helper collections are exposed as getters, so only the helpers you want will be required and loaded.

var helpers = require('handlebars-helpers');
var math = helpers.math();
//=> only the `math` helpers

var helpers = require('handlebars-helpers');
var array = helpers.array();
//=> only the `collections` helpers

Optionally pass your own handlebars

var handlebars = require('handlebars');
var helpers = require('handlebars-helpers')({
  handlebars: handlebars
});

// or for a specific collection
var math = helpers.math({
  handlebars: handlebars
});

Helpers

Categories

Currently 141 helpers in 19 categories:

All helpers

array helpers

Visit the: code | unit tests | issues)

code helpers

Visit the: code | unit tests | issues)

collection helpers

Visit the: code | unit tests | issues)

comparison helpers

Visit the: code | unit tests | issues)

date helpers

Visit the: code | unit tests | issues)

fs helpers

Visit the: code | unit tests | issues)

html helpers

Visit the: code | unit tests | issues)

i18n helpers

Visit the: code | unit tests | issues)

inflection helpers

Visit the: code | unit tests | issues)

logging helpers

Visit the: code | unit tests | issues)

markdown helpers

Visit the: code | unit tests | issues)

match helpers

Visit the: code | unit tests | issues)

math helpers

Visit the: code | unit tests | issues)

misc helpers

Visit the: code | unit tests | issues)

number helpers

Visit the: code | unit tests | issues)

object helpers

Visit the: code | unit tests | issues)

path helpers

Visit the: code | unit tests | issues)

string helpers

Visit the: code | unit tests | issues)

url helpers

Visit the: code | unit tests | issues)


array

{{after}}

Returns all of the items in an array after the specified index. Opposite of before.

Params

  • array {Array}: Collection
  • n {Number}: Starting index (number of items to exclude)
  • returns {Array}: Array exluding n items.

Example

{{after "['a', 'b', 'c']" 1}}
//=> '["c"]'

{{arrayify}}

Cast the given value to an array.

Params

  • value {any}
  • returns {Array}

Example

{{arrayify "foo"}}
//=> '["foo"]'

{{before}}

Return all of the items in the collection before the specified count. Opposite of after.

Params

  • array {Array}
  • n {Number}
  • returns {Array}: Array excluding items after the given number.

Example

{{before "['a', 'b', 'c']" 2}}
//=> '["a", "b"]'

{{eachIndex}}

Params

  • array {Array}
  • options {Object}
  • returns {String}

Example

{{#eachIndex collection}}
  {{item}} is {{index}}
{{/eachIndex}}

{{filter}}

Block helper that filters the given array and renders the block for values that evaluate to true, otherwise the inverse block is returned.

Params

  • array {Array}
  • value {any}
  • options {Object}
  • returns {String}

Example

{{#filter array "foo"}}AAA{{else}}BBB{{/filter}}
//=> 'BBB

{{first}}

Returns the first item, or first n items of an array.

Params

  • array {Array}
  • n {Number}: Number of items to return, starting at 0.
  • returns {Array}

Example

{{first "['a', 'b', 'c', 'd', 'e']" 2}}
//=> '["a", "b"]'

{{forEach}}

Iterates over each item in an array and exposes the current item in the array as context to the inner block. In addition to the current array item, the helper exposes the following variables to the inner block:

  • index
  • total
  • isFirst
  • isLast Also, @index is exposed as a private variable, and additional private variables may be defined as hash arguments.

Params

  • array {Array}
  • returns {String}

Example

var accounts = [
{'name': 'John', 'email': 'john@example.com'},
{'name': 'Malcolm', 'email': 'malcolm@example.com'},
{'name': 'David', 'email': 'david@example.com'}
];

// example usage
// {{#forEach accounts}}
//   <a href="mailto:{{ email }}" title="Send an email to {{ name }}">
//     {{ name }}
//   </a>{{#unless isLast}}, {{/unless}}
// {{/forEach}}

{{inArray}}

Block helper that renders the block if an array has the given value. Optionally specify an inverse block to render when the array does not have the given value.

Given the array ['a', 'b', 'c']:

Params

  • array {Array}
  • value {any}
  • options {Object}
  • returns {String}

Example

{{#inArray array "d"}}
  foo
{{else}}
  bar
{{/inArray}}

{{isArray}}

Returns true if value is an es5 array.

Params

  • value {any}: The value to test.
  • returns {Boolean}

Example

{{isArray "abc"}}
//=> 'false'

{{join}}

Join all elements of array into a string, optionally using a given separator.

Params

  • array {Array}
  • sep {String}: The separator to use.
  • returns {String}

Example

{{join "['a', 'b', 'c']"}}
//=> 'a, b, c'

{{join "['a', 'b', 'c']" '-'}}
//=> 'a-b-c'

{{last}}

Returns the last item, or last n items of an array. Opposite of first.

Params

  • array {Array}
  • n {Number}: Number of items to return, starting with the last item.
  • returns {Array}

Example

{{last "['a', 'b', 'c', 'd', 'e']" 2}}
//=> '["d", "e"]'

{{lengthEqual}}

Block helper that compares the length of the given array to the number passed as the second argument. If the array length is equal to the given length, the block is returned, otherwise an inverse block may optionally be returned.

Params

  • array {Array}
  • length {Number}
  • options {Object}
  • returns {String}

{{map}}

Returns a new array, created by calling function on each element of the given array.

Params

  • array {Array}
  • fn {Function}
  • returns {String}

Example

// register `double` as a helper
function double(str) {
  return str + str;
}
// then used like this:
// {{map "['a', 'b', 'c']" double}}
//=> '["aa", "bb", "cc"]'

{{some}}

Block helper that returns the block if the callback returns true for some value in the given array.

Params

  • array {Array}
  • cb {Function}: callback function
  • {Options}: Handlebars provided options object
  • returns {Array}

Example

{{#some array isString}}
  Render me if the array has a string.
{{else}}
  Render me if it doesn't.
{{/some}}

{{sort}}

Sort the given array. If an array of objects is passed, you may optionally pass a key to sort on as the second argument. You may alternatively pass a sorting function as the second argument.

Params

  • array {Array}: the array to sort.
  • key {String|Function}: The object key to sort by, or sorting function.

Example

{{sort "['b', 'a', 'c']"}}
//=> 'a,b,c'

{{sortBy}}

Sort an array. If an array of objects is passed, you may optionally pass a key to sort on as the second argument. You may alternatively pass a sorting function as the second argument.

Params

  • array {Array}: the array to sort.
  • props {String|Function}: One or more properties to sort by, or sorting functions to use.

Example

{{sortBy '["b", "a", "c"]'}}
//=> 'a,b,c'

{{sortBy '[{a: "zzz"}, {a: "aaa"}]' "a"}}
//=> '[{"a":"aaa"},{"a":"zzz"}]'

{{withAfter}}

Use the items in the array after the specified index as context inside a block. Opposite of withBefore.

Params

  • array {Array}
  • idx {Number}
  • options {Object}
  • returns {Array}

{{withBefore}}

Use the items in the array before the specified index as context inside a block.Opposite of withAfter.

Params

  • array {Array}
  • idx {Number}
  • options {Object}
  • returns {Array}

Example

{{#withBefore array 3}}
  {{this}}
{{/withBefore}}

{{withFirst}}

Use the first item in a collection inside a handlebars block expression. Opposite of withLast.

Params

  • array {Array}
  • idx {Number}
  • options {Object}
  • returns {String}

{{withLast}}

Use the last item or n items in an array as context inside a block. Opposite of withFirst.

Params

  • array {Array}
  • idx {Number}: The starting index.
  • options {Object}
  • returns {String}

{{withSort}}

Block helper that sorts a collection and exposes the sorted collection as context inside the block.

Params

  • array {Array}
  • prop {String}
  • options {Object}: Specify reverse="true" to reverse the array.
  • returns {String}

code

{{embed}}

Embed code from an external file as preformatted text.

Params

  • fp {String}: filepath to the file to embed.
  • language {String}: Optionally specify the language to use for syntax highlighting.
  • returns {String}

Example

{{embed 'path/to/file.js'}}

// specify the language to use
{{embed 'path/to/file.hbs' 'html')}}

{{gist}}

Embed a GitHub Gist using only the id of the Gist

Params

  • id {String}
  • returns {String}

Example

{{gist 12345}}

{{jsfiddle}}

Generate the HTML for a jsFiddle link with the given params

Params

  • params {Object}
  • returns {String}

Example

{{jsfiddle id="0dfk10ks" tabs="true"}}

collection

{{isEmpty}}

Block helper that returns a block if the given collection is empty. If the collection is not empty the inverse block is returned (if supplied).

Params

  • collection {Object}
  • options {Object}
  • returns {String}

{{iterate}}

Iterate over an array or object,

Params

  • context {Object|Array}: The collection to iterate over
  • options {Object}
  • returns {String}

{{length}}

Returns the length of the given collection.

Params

  • value {Array|Object|String}
  • returns {Number}: The length of the value.

Example

{{length "['a', 'b', 'c']"}}
//=> 3

comparison

{{and}}

Block helper that renders the block if both of the given values are truthy. If an inverse block is specified it will be rendered when falsy.

Params

  • a {any}
  • b {any}
  • options {Object}: Handlebars provided options object
  • returns {String}

{{compare}}

Render a block when a comparison of the first and third arguments returns true. The second argument is the arithemetic operator to use. You may also optionally specify an inverse block to render when falsy.

Params

  • a {}
  • operator {}: The operator to use. Operators must be enclosed in quotes: ">", "=", "<=", and so on.
  • b {}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or if specified the inverse block is rendered if falsey.

{{contains}}

Block helper that renders the block if collection has the given value, using strict equality (===) for comparison, otherwise the inverse block is rendered (if specified). If a startIndex is specified and is negative, it is used as the offset from the end of the collection.

Given the array ['a', 'b', 'c']:

Params

  • collection {Array|Object|String}: The collection to iterate over.
  • value {any}: The value to check for.
  • [startIndex=0] {Number}: Optionally define the starting index.
  • options {Object}: Handlebars provided options object.

Example

{{#contains array "d"}}
  This will not be rendered.
{{else}}
  This will be rendered.
{{/contains}}

{{gt}}

Block helper that renders a block if a is greater than b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

  • a {String}
  • b {String}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{gte}}

Block helper that renders a block if a is greater than or equal to b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

  • a {String}
  • b {String}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{has}}

Block helper that renders a block if value has pattern. If an inverse block is specified it will be rendered when falsy.

Params

  • val {any}: The value to check.
  • pattern {any}: The pattern to check for.
  • options {Object}: Handlebars provided options object
  • returns {String}

{{eq}}

Block helper that renders a block if a is equal to b. If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

  • a {String}
  • b {String}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{ifEven}}

Return true if the given value is an even number.

Params

  • number {Number}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

Example

{{#ifEven value}}
  render A
{{else}}
  render B
{{/ifEven}}

{{ifNth}}

Conditionally renders a block if the remainder is zero when a operand is divided by b. If an inverse block is specified it will be rendered when the remainder is not zero.

Params

  • {}: {Number}
  • {}: {Number}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{ifOdd}}

Block helper that renders a block if value is an odd number. If an inverse block is specified it will be rendered when falsy.

Params

  • value {Object}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

Example

{{#ifOdd value}}
  render A
{{else}}
  render B
{{/ifOdd}}

{{is}}

Block helper that renders a block if a is equal to b. If an inverse block is specified it will be rendered when falsy.

Params

  • a {any}
  • b {any}
  • options {Object}: Handlebars provided options object
  • returns {String}

{{isnt}}

Block helper that renders a block if a is not equal to b. If an inverse block is specified it will be rendered when falsy.

Params

  • a {String}
  • b {String}
  • options {Object}: Handlebars provided options object
  • returns {String}

{{lt}}

Block helper that renders a block if a is less than b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

  • context {Object}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{lte}}

Block helper that renders a block if a is less than or equal to b.

If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value.

Params

  • a {Sring}
  • b {Sring}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{neither}}

Block helper that renders a block if neither of the given values are truthy. If an inverse block is specified it will be rendered when falsy.

Params

  • a {any}
  • b {any}
  • options {}: Handlebars options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{or}}

Block helper that renders a block if any of the given values is truthy. If an inverse block is specified it will be rendered when falsy.

Params

  • arguments {...any}: ,
  • options {}: Handlebars options object
  • returns {String}: Block, or inverse block if specified and falsey.

Example

{{#or a b c}}
  If any value is true this will be rendered.
{{/or}}

{{unlessEq}}

Block helper that always renders the inverse block unless a is is equal to b.

Params

  • a {String}
  • b {String}
  • options {Object}: Handlebars provided options object
  • returns {String}: Inverse block by default, or block if falsey.

{{unlessGt}}

Block helper that always renders the inverse block unless a is is greater than b.

Params

  • context {Object}
  • options {Object}: Handlebars provided options object
  • returns {String}: Inverse block by default, or block if falsey.

{{unlessLt}}

Block helper that always renders the inverse block unless a is is less than b.

Params

  • context {Object}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{unlessGteq}}

Block helper that always renders the inverse block unless a is is greater than or equal to b.

Params

  • context {Object}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

{{unlessLteq}}

Block helper that always renders the inverse block unless a is is less than or equal to b.

Params

  • context {Object}
  • options {Object}: Handlebars provided options object
  • returns {String}: Block, or inverse block if specified and falsey.

date

{{moment}}

Expose moment helper

fs

{{fileSize}}

Converts bytes into a nice representation with unit.

Examples:

  • 13661855 => 13.7 MB
  • 825399 => 825 KB
  • 1396 => 1 KB

Params

  • value {String}
  • returns {String}

{{read}}

Read a file from the file system.

Params

  • filepath {String}
  • returns {String}

{{readdir}}

Return an array of files from the given directory.

Params

  • directory {String}
  • returns {Array}

html

{{css}}

Add an array of <link> tags. Automatically resolves relative paths to options.assets if passed on the context.

Params

  • context {Object}
  • returns {String}

{{ellipsis}}

Truncates a string to the specified length, and appends it with an elipsis, .

Params

  • str {String}
  • length {Number}: The desired length of the returned string.
  • returns {String}: The truncated string.

Example

{{ellipsis "<span>foo bar baz</span>", 7}}
//=> 'foo bar…'

{{js}}

Generate one or more <script></script> tags with paths/urls to javascript or coffeescript files.

Params

  • context {Object}
  • returns {String}

Example

{{js scripts}}

{{sanitize}}

Strip HTML tags from a string, so that only the text nodes are preserved.

Params

  • str {String}: The string of HTML to sanitize.
  • returns {String}

Example

{{sanitize "<span>foo</span>"}}
//=> 'foo'

{{truncate}}

Truncate a string by removing all HTML tags and limiting the result to the specified length. Aslo see ellipsis.

Params

  • str {String}
  • limit {Number}: The desired length of the returned string.
  • suffix {String}: Optionally supply a string to use as a suffix to denote when the string has been truncated.
  • returns {String}: The truncated string.

Example

truncate("<span>foo bar baz</span>", 7);
//=> 'foo bar'

{{ul}}

Block helper for creating unordered lists (<ul></ul>)

Params

  • context {Object}
  • options {Object}
  • returns {String}

{{ol}}

Block helper for creating ordered lists (<ol></ol>)

Params

  • context {Object}
  • options {Object}
  • returns {String}

{{thumbnailImage}}

Returns a <figure> with a thumbnail linked to a full picture

Params

  • context {Object}: Object with values/attributes to add to the generated elements:
  • context.alt {String}
  • context.src {String}
  • context.width {Number}
  • context.height {Number}
  • returns {String}: HTML <figure> element with image and optional caption/link.

i18n

{{i18n}}

i18n helper. See button-i18n for a working example.

Params

  • key {String}
  • options {Object}
  • returns {String}

inflection

{{inflect}}

Params

  • count {Number}
  • singular {String}: The singular form
  • plural {String}: The plural form
  • include {String}
  • returns {String}

{{ordinalize}}

Returns an ordinalized number (as a string).

Params

  • val {String}: The value to ordinalize.
  • returns {String}: The ordinalized number

Example

{{ordinalize 1}}
//=> '1st'
{{ordinalize 21}}
//=> '21st'
{{ordinalize 29}}
//=> '29th'
{{ordinalize 22}}
//=> '22nd'

logging

logging-helpers.

markdown

{{markdown}}

Block helper that converts a string of inline markdown to HTML.

Params

  • context {Object}
  • options {Object}
  • returns {String}

Example

{{#markdown}}
# Foo
{{/markdown}}
//=> <h1>Foo</h1>

{{md}}

Read a markdown file from the file system and inject its contents after converting it to HTML.

Params

  • context {Object}
  • options {Object}
  • returns {String}

Example

{{md "foo/bar.md"}}

match

{{mm}}

The main function. Pass an array of filepaths, and a string or array of glob patterns. Options may be passed on the hash or on this.options.

Params

  • files {Array|String}
  • patterns {Array|String}: One or more glob patterns.
  • options {Object}
  • returns {Array}: Array of matches

Example

{{match (readdir "foo") "*.js"}}

{{match}}

Returns an array of files that match the given glob pattern. Options may be passed on the hash or on this.options.

Params

  • files {Array}
  • pattern {String}
  • options {Object}
  • returns {Array}

Example

{{match (readdir "foo") "*.js"}}

{{isMatch}}

Returns true if a filepath contains the given pattern. Options may be passed on the hash or on this.options.

Params

  • filepath {String}
  • pattern {String}
  • options {Object}
  • returns {Boolean}

Example

{{isMatch "foo.md" "*.md"}}
//=> true

math

{{add}}

Return the product of a plus b.

Params

  • a {Number}
  • b {Number}

{{subtract}}

Return the product of a minus b.

Params

  • {Number}: a

{{divide}}

Divide a by b

Params

  • a {Number}: numerator
  • b {Number}: denominator

{{multiply}}

Multiply a by b.

Params

  • a {Number}: factor
  • b {Number}: multiplier

{{floor}}

Get the Math.floor() of the given value.

Params

  • value {Number}

{{ceil}}

Get the Math.ceil() of the given value.

Params

  • value {Number}

{{round}}

Round the given value.

Params

  • value {Number}

{{sum}}

Returns the sum of all numbers in the given array.

Params

  • array {Array}: Array of numbers to add up.
  • returns {Number}

Example

{{sum "[1, 2, 3, 4, 5]"}}
//=> '15'

{{avg}}

Returns the average of all numbers in the given array.

Params

  • array {Array}: Array of numbers to add up.
  • returns {Number}

Example

{{avg "[1, 2, 3, 4, 5]"}}
//=> '3'

misc

{{default}}

Returns the first value if defined, otherwise the "default" value is returned.

Params

  • value {any}
  • defaultValue {any}
  • returns {String}

{{option}}

Return the given value of prop from this.options. Given the context {options: {a: {b: {c: 'ddd'}}}}

Params

  • prop {String}
  • returns {any}

Example

{{option "a.b.c"}}
<!-- results => `ddd` -->

{{noop}}

Block helper that renders the block without taking any arguments.

Params

  • options {Object}
  • returns {String}

{{withHash}}

Block helper that builds the context for the block from the options hash.

Params

  • options {Object}: Handlebars provided options object.

number

{{addCommas}}

Add commas to numbers

Params

  • num {Number}
  • returns {Number}

{{phoneNumber}}

Convert a string or number to a formatted phone number.

Params

  • num {Number|String}: The phone number to format, e.g. 8005551212
  • returns {Number}: Formatted phone number: (800) 555-1212

{{random}}

Generate a random number between two values

Params

  • min {Number}
  • max {Number}
  • returns {String}

{{toAbbr}}

Abbreviate numbers to the given number of precision.

Params

  • number {String}
  • precision {String}
  • returns {String}

{{toExponential}}

Returns a string representing the given number in exponential notation.

Params

  • number {Number}
  • fractionDigits {Number}: Optional. An integer specifying the number of digits to use after the decimal point. Defaults to as many digits as necessary to specify the number.
  • returns {Number}

Example

{{toExponential number digits}};

{{toFixed}}

Formats the given number using fixed-point notation.

Params

  • number {Number}
  • digits {Number}: Optional. The number of digits to use after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.
  • returns {Number}

{{toFloat}}

Params

  • number {Number}
  • returns {Number}

{{toInt}}

Params

  • number {Number}
  • returns {Number}

{{toPrecision}}

Params

  • number {Number}
  • returns {Number}

object

{{extend}}

Extend the context with the properties of other objects. A shallow merge is performed to avoid mutating the context.

Params

  • objects {Object}: One or more objects to extend.
  • returns {Object}

{{forIn}}

Block helper that iterates over the properties of an object, exposing each key and value on the context.

Params

  • context {Object}
  • options {Object}
  • returns {String}

{{forOwn}}

Block helper that iterates over the own properties of an object, exposing each key and value on the context.

Params

  • obj {Object}: The object to iterate over.
  • options {Object}
  • returns {String}

{{get}}

Use property paths (a.b.c) to get a value or nested value from the context. Works as a regular helper or block helper.

Params

  • prop {String}: The property to get, optionally using dot notation for nested properties.
  • context {Object}: The context object
  • options {Object}: The handlebars options object, if used as a block helper.
  • returns {String}

{{getObject}}

Use property paths (a.b.c) to get an object from the context. Differs from the get helper in that this helper will return the actual object, including the given property key. Also, this helper does not work as a block helper.

Params

  • prop {String}: The property to get, optionally using dot notation for nested properties.
  • context {Object}: The context object
  • returns {String}

{{hasOwn}}

Return true if key is an own, enumerable property of the given context object.

Params

  • key {String}
  • context {Object}: The context object.
  • returns {Boolean}

Example

{{hasOwn context key}}

{{isObject}}

Return true if value is an object.

Params

  • value {String}
  • returns {Boolean}

Example

{{isObject "foo"}}
//=> false

{{merge}}

Deeply merge the properties of the given objects with the context object.

Params

  • object {Object}: The target object. Pass an empty object to shallow clone.
  • objects {Object}
  • returns {Object}

{{parseJSON}}

Block helper that parses a string using JSON.parse, then passes the parsed object to the block as context.

Params

  • string {String}: The string to parse
  • options {Object}: Handlebars options object

{{pick}}

Pick properties from the context object.

Params

  • properties {Array|String}: One or more proeperties to pick.
  • context {Object}
  • options {Object}: Handlebars options object.
  • returns {Object}: Returns an object with the picked values. If used as a block helper, the values are passed as context to the inner block. If no values are found, the context is passed to the inverse block.

{{stringify}}

Stringify an object using JSON.stringify.

Params

  • obj {Object}: Object to stringify
  • returns {String}

path

{{absolute}}

Get the directory path segment from the given filepath.

Params

  • ext {String}
  • returns {String}

Example

{{absolute "docs/toc.md"}}
//=> 'docs'

{{dirname}}

Get the directory path segment from the given filepath.

Params

  • ext {String}
  • returns {String}

Example

{{dirname "docs/toc.md"}}
//=> 'docs'

{{relative}}

Get the relative filepath from a to b.

Params

  • a {String}
  • b {String}
  • returns {String}

Example

{{relative a b}}

{{basename}}

Get the file extension from the given filepath.

Params

  • ext {String}
  • returns {String}

Example

{{basename "docs/toc.md"}}
//=> 'toc.md'

{{stem}}

Get the "stem" from the given filepath.

Params

  • filepath {String}
  • returns {String}

Example

{{stem "docs/toc.md"}}
//=> 'toc'

{{extname}}

Get the file extension from the given filepath.

Params

  • filepath {String}
  • returns {String}

Example

{{extname "docs/toc.md"}}
//=> '.md'

{{segments}}

Get specific (joined) segments of a file path by passing a range of array indices.

Params

  • filepath {String}: The file path to split into segments.
  • returns {String}: Returns a single, joined file path.

Example

{{segments "a/b/c/d" "2" "3"}}
//=> 'c/d'

{{segments "a/b/c/d" "1" "3"}}
//=> 'b/c/d'

{{segments "a/b/c/d" "1" "2"}}
//=> 'b/c'

string

{{camelcase}}

camelCase the characters in the given string.

Params

  • string {String}: The string to camelcase.
  • returns {String}

Example

{{camelcase "foo bar baz"}};
//=> 'fooBarBaz'

{{capitalize}}

Capitalize the first word in a sentence.

Params

  • str {String}
  • returns {String}

Example

{{capitalize "foo bar baz"}}
//=> "Foo bar baz"

{{capitalizeAll}}

Capitalize all words in a string.

Params

  • str {String}
  • returns {String}

Example

{{capitalize "foo bar baz"}}
//=> "Foo Bar Baz"

{{center}}

Center a string using non-breaking spaces

Params

  • str {String}
  • spaces {String}
  • returns {String}

{{chop}}

Like trim, but removes both extraneous whitespace and non-word characters from the beginning and end of a string.

Params

  • string {String}: The string to chop.
  • returns {String}

Example

{{chop "_ABC_"}}
//=> 'ABC'

{{chop "-ABC-"}}
//=> 'ABC'

{{chop " ABC "}}
//=> 'ABC'

{{dashcase}}

dash-case the characters in string. Replaces non-word characters and periods with hyphens.

Params

  • string {String}
  • returns {String}

Example

{{dashcase "a-b-c d_e"}}
//=> 'a-b-c-d-e'

{{dotcase}}

dot.case the characters in string.

Params

  • string {String}
  • returns {String}

Example

{{dotcase "a-b-c d_e"}}
//=> 'a.b.c.d.e'

{{hyphenate}}

Replace spaces in a string with hyphens.

Params

  • str {String}
  • returns {String}

Example

{{hyphenate "foo bar baz qux"}}
//=> "foo-bar-baz-qux"

{{isString}}

Return true if value is a string.

Params

  • value {String}
  • returns {Boolean}

Example

{{isString "foo"}}
//=> 'true'

{{lowercase}}

Lowercase all characters in the given string.

Params

  • str {String}
  • returns {String}

Example

{{lowercase "Foo BAR baZ"}}
//=> 'foo bar baz'

{{occurrences}}

Return the number of occurrances of substring within the given string.

Params

  • str {String}
  • substring {String}
  • returns {Number}: Number of occurrances

Example

{{occurrances "foo bar foo bar baz" "foo"}}
//=> 2

{{pascalcase}}

PascalCase the characters in string.

Params

  • string {String}
  • returns {String}

Example

{{pascalcase "foo bar baz"}}
//=> 'FooBarBaz'

{{pathcase}}

path/case the characters in string.

Params

  • string {String}
  • returns {String}

Example

{{pathcase "a-b-c d_e"}}
//=> 'a/b/c/d/e'

{{plusify}}

Replace spaces in the given string with pluses.

Params

  • str {String}: The input string
  • returns {String}: Input string with spaces replaced by plus signs

Example

{{plusify "foo bar baz"}}
//=> 'foo+bar+baz'

{{reverse}}

Reverse a string.

Params

  • str {String}
  • returns {String}

Example

{{reverse "abcde"}}
//=> 'edcba'

{{replace}}

Replace all occurrences of a with b.

Params

  • str {String}
  • a {String}
  • b {String}
  • returns {String}

Example

{{replace "a b a b a b" "a" "z"}}
//=> 'z b z b z b'

{{sentence}}

Sentence case the given string

Params

  • str {String}
  • returns {String}

Example

{{sentence "hello world. goodbye world."}}
//=> 'Hello world. Goodbye world.'

{{snakecase}}

snake_case the characters in the given string.

Params

  • string {String}
  • returns {String}

Example

{{snakecase "a-b-c d_e"}}
//=> 'a_b_c_d_e'

{{split}}

Split string by the given character.

Params

  • string {String}: The string to split.
  • returns {String} character: Default is ,

Example

{{split "a,b,c" ","}}
//=> ['a', 'b', 'c']

{{startsWith}}

Tests whether a string begins with the given prefix.

Params

  • prefix {String}
  • testString {String}
  • options {String}
  • returns {String}

Example

{{#startsWith "Goodbye" "Hello, world!"}}
  Whoops
{{else}}
  Bro, do you even hello world?
{{/startsWith}}

{{titleize}}

Title case the given string.

Params

  • str {String}
  • returns {String}

Example

{{titleize "this is title case"}}
//=> 'This Is Title Case'

{{trim}}

Removes extraneous whitespace from the beginning and end of a string.

Params

  • string {String}: The string to trim.
  • returns {String}

Example

{{trim " ABC "}}
//=> 'ABC'

{{uppercase}}

Uppercase all of the characters in the given string. If used as a block helper it will uppercase the entire block. This helper does not support inverse blocks.

Params

  • str {String}: The string to uppercase
  • options {Object}: Handlebars options object
  • returns {String}

url

{{encodeURI}}

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character.

Params

  • str {String}: The un-encoded string
  • returns {String}: The endcoded string

{{decodeURI}}

Decode a Uniform Resource Identifier (URI) component.

Params

  • str {String}
  • returns {String}

{{urlResolve}}

Take a base URL, and a href URL, and resolve them as a browser would for an anchor tag.

Params

  • base {String}
  • href {String}
  • returns {String}

{{urlParse}}

Parses a url string into an object.

Params

  • str {String}: URL string
  • returns {String}: Returns stringified JSON

{{stripQuerystring}}

Strip the query string from the give url.

Params

  • url {String}
  • returns {String}

Utils

The following utils are exposed on .utils.

{{toRegex}}

Converts a "regex-string" to an actual regular expression.

Params

  • value {Object}
  • returns {Boolean}

Example

utils.toRegex('"/foo/"');
//=> /foo/

{{isRegex}}

Returns true if the given value appears to be a regular expression.

Params

  • value {Object}
  • returns {Boolean}

{{changecase}}

Change casing on the given string, optionally passing a delimiter to use between words in the returned string.

Params

  • string {String}: The string to change.
  • returns {String}

Example

utils.changecase('fooBarBaz');
//=> 'foo bar baz'

utils.changecase('fooBarBaz' '-');
//=> 'foo-bar-baz'

{{random}}

Generate a random number

Params

  • min {Number}
  • max {Number}
  • returns {Number}

{{isUndefined}}

Returns true if the given value is undefined or is a handlebars options hash.

Params

  • value {any}
  • returns {Boolean}

{{isOptions}}

Returns true if the given value appears to be an options object.

Params

  • value {Object}
  • returns {Boolean}

{{getArgs}}

Get options from the options hash and this.

Params

  • app {Object}: The current application instance.
  • returns {Object}

{{isObject}}

Returns true if the given value is an object and not an array.

Params

  • value {any}
  • returns {Boolean}

{{isEmpty}}

Returns true if the given value is empty.

Params

  • value {any}
  • returns {Boolean}

{{tryParse}}

Try to parse the given string as JSON. Fails gracefully if the value cannot be parsed.

Params

  • string {String}
  • returns {Object}

{{result}}

Return the given value. If the value is a function it will be called, and the result is returned.

Params

  • val {any}
  • returns {any}

{{identity}}

Return the given value, unchanged.

Params

  • val {any}
  • returns {any}

{{isString}}

Return true if val is a string.

Params

  • val {any}: The value to check
  • returns {Boolean}

{{arrayify}}

Cast val to an array.

Params

  • val {any}: The value to arrayify.
  • returns {Array}

About

Related projects

  • assemble: Get the rocks out of your socks! Assemble makes you fast at creating web projects… more | homepage
  • template-helpers: Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… more | homepage
  • utils: Fast, generic JavaScript/node.js utility functions. | homepage

Contributing

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

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Brian Woodward

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. When this project was created some helpers were sourced from Swag, by Elving Rodriguez. Released under the MIT license.


This file was generated by verb-generate-readme, v0.1.30, on August 17, 2016.