Skip to content

188 handlebars helpers in ~20 categories. Can be used with Assemble, Ghost, YUI, express.js etc.

License

Notifications You must be signed in to change notification settings

MaxMood96/handlebars-helpers

Repository files navigation

handlebars-helpers NPM version Build Status

More than 130 Handlebars helpers in ~20 categories that can be used with any Handlebars project.

(Table of contents generated by verb)

Install

Install with npm

$ npm i handlebars-helpers --save

Usage

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

array

Params

  • array {Array}
  • options {Object}

Returns all of the items in an arry 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"]'

Cast val to an array.

Params

  • val {*}: The value to arrayify.
  • returns {Array}: An array.
  • returns {Array}

Example

{{arrayify "abc"}}
//=> '["a"]'

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"]'

Params

  • array {type}
  • value {type}
  • options {type}
  • returns {String}

Params

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

Example

{{#eachIndexPlusOne array}}
  {{item}} is {{index}}
{{/eachIndexPlusOne}}

Params

  • array {type}
  • options {type}
  • returns {String}

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"]'
// example usage
// {{#forEach accounts}}
//   <a href="mailto:{{ email }}" title="Send an email to {{ name }}">
//     {{ name }}
//   </a>{{#unless isLast}}, {{/unless}}
// {{/forEach}}

Params

  • array {type}
  • value {type}
  • options {type}
  • returns {String}

Returns true if value is an array.

Params

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

Example

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

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'

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"]'

Returns the length of the given array.

Params

  • array {Array}
  • returns {Number}: The length of the array.

Example

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

Params

  • array {type}
  • length {type}
  • options {type}
  • returns {String}

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

Assuming that double has been registered as a helper:

Params

  • array {Array}
  • fn {String}: The function to
  • returns {String}

Examples

function double(str) {
  return str + str;
}
{{map "['a', 'b', 'c']" double}}
//=> '["aa", "bb", "cc"]'

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'

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

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

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

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

Shuffle the items in an array.

Params

  • arr {Array}
  • returns {Array}

Example

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

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

Params

  • array {Array}
  • {Number}: count
  • options {Object}
  • returns {*}

Use the last item in a collection inside a block. Opposite of [withFirst][].

Params

  • array {Array}
  • {Number}: count
  • options {Object}
  • returns {*}

Params

  • array {type}
  • field {type}
  • options {type}
  • returns {String}

Converts a string such as "foo, bar, baz" to an ES Array of strings.

Params

  • str {String}
  • returns {String}

Use all of the items in the collection after the specified count inside a block.

Params

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

Use all of the items in the collection before the specified count inside a block. Opposite of {{withAfter}}

Params

  • array {Array}
  • {Number}: count
  • options {Object}
  • returns {Array}

code

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')}}

Generate the HTML for a jsFiddle link with the given params

Params

  • params {Object}
  • returns {String}

Example

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

comparison

Checks if a given value is present in a collection using strict equality for comparisons, i.e. ===. If fromIndex is negative, it is used as the offset from the end of the collection.

Params

  • collection {Array|Object|string}: The collection to iterate over.
  • target {*}: The value to check for.
  • [fromIndex=0] {Number}: Optionally define the index to search from.

Returns true if a value exists in the given string, array or object, or if a callback is passed checks to see if a truthy value is returned for any element in a collection or for any sub-string in a string.

Params

  • val {*}: The value to check.
  • pattern {*}: The pattern to check for.
  • options {Object}
  • returns {Boolean}

Params

  • a {type}
  • b {type}
  • options {type}
  • returns {Boolean}

Params

  • value {type}
  • test {type}
  • options {type}
  • returns {Boolean}

Params

  • value {type}
  • test {type}
  • options {type}
  • returns {Boolean}

Params

  • value {type}
  • test {type}
  • options {type}
  • returns {Boolean}

Params

  • value {type}
  • test {type}
  • options {type}
  • returns {Boolean}

Params

  • value {type}
  • test {type}
  • options {type}
  • returns {Boolean}

Params

  • value {type}
  • test {type}
  • options {type}
  • returns {Boolean}

Params

  • context {type}
  • options {type}
  • returns {Boolean}

Params

  • context {type}
  • options {type}
  • returns {Boolean}

Params

  • context {type}
  • options {type}
  • returns {Boolean}

Params

  • context {type}
  • options {type}
  • returns {Boolean}

data

JSON.stringify exposed as a helper.

Params

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

date

fs

html

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

Params

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

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

Params

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

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

index

inflection

Params

  • count {type}
  • singular {type}
  • plural {type}
  • include {type}
  • returns {String}

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

markdown

math

Return the product of a plus b.

Params

  • a {Number}
  • b {Number}

Return the product of a minus b.

Params

  • {Number}: a

Divide a by b

Params

  • a {Number}
  • b {Number}: divisor

Divide a by b.

Params

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

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

Params

  • value {Number}

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

Params

  • value {Number}

Round the given value.

Params

  • value {Number}

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'

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

Params

  • value {type}
  • defaultValue {type}
  • returns {String}

Params

  • options {type}
  • returns {String}

number

Output a formatted phone number

Params

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

Uses [randomatic] to generate a randomized string based on the given parameters.

See the [randomatic] docs for the full range of options.

  • returns {String}

Abbreviate numbers to the given number of digits.

Params

  • number {String}
  • digits {String}
  • returns {String}

Params

  • number {type}
  • fractions {type}
  • returns {Number}

Params

  • number {type}
  • digits {type}
  • returns {Number}

object

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}

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

Params

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

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

Params

  • key {String}
  • obj {Object}: The object to check.
  • returns {Boolean}

Example

{{hasOwn obj key}}

Return true if value is an object.

Params

  • value {String}
  • returns {Boolean}

Example

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

Recursively combine the properties of o with the properties of other objects.

Params

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

path

Get the relative path from a to b.

Params

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

Example

{{relative a b}}

Params

  • ext {String}
  • returns {String}

Example

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

string

Capitalize first word in a sentence

Params

  • str {String}
  • returns {String}

Capitalize the first word in a sentence

Params

  • str {String}
  • returns {String}

Capitalize each word in a sentence

Params

  • str {String}
  • returns {String}

Capitalize each word in a sentence

Params

  • str {String}
  • returns {String}

Center a string using non-breaking spaces

Params

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

Replace periods in string with hyphens.

Params

  • {String}: str
  • returns {String}

Replace spaces in string with hyphens.

Params

  • {String}: str
  • returns {String}

Make all letters in the string lowercase

Params

  • {String}: str
  • returns {String}

Replace spaces in string with pluses.

Params

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

Sentence case

Params

  • {String}: str
  • returns {String}

Title case. "This is Title Case"

Params

  • {String}: str
  • returns {String}

Params

  • options {type}
  • returns {String}

Params

  • str {type}
  • returns {String}

Return the number of occurrances of a string, within a string

Params

  • {String}: str The haystack
  • {String}: substring The needle
  • returns {Number}: The number of times the needle is found in the haystack.

Replace occurrences of string "A" with string "B"

Params

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

Truncate the input string and removes all HTML tags

Params

  • {String}: str The input string.
  • {Number}: limit The number of characters to limit the string.
  • {String}: append The string to append if charaters are omitted.
  • returns {String}: The truncated string.

Truncates a string given a specified length, providing a custom string to denote an omission.

Params

  • {String}: str
  • {String}: length
  • {String}: omission
  • returns {String}

url

Params

  • url {type}
  • returns {String}

Contributing

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

Tests

Install dev dependencies:

$ npm i -d && npm test

Authors

Jon Schlinkert

Brian Woodward

Related projects

  • assemble: Static site generator for Grunt.js, Yeoman and Node.js. Used by Zurb Foundation, Zurb Ink, H5BP/Effeckt,… more | homepage
  • template-helpers: Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… more | homepage

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

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

If this project doesn't do what you need, please let us know!

Author

Jon Schlinkert

License

Copyright © 2014-2015 Jon Schlinkert When this library was initially created a number of the helpers were sourced from Swag, by Elving Rodriguez. Thanks, Elving, for your hard work on Swag. Released under the MIT license.


This file was generated by verb-cli on September 24, 2015.

About

188 handlebars helpers in ~20 categories. Can be used with Assemble, Ghost, YUI, express.js etc.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.9%
  • HTML 0.1%