Skip to main content

Handlebars helpers

What are Handlebars Helpers?

Handlebars helpers are custom functions you can use inside your Handlebars templates to add logic, format data, or perform calculations—right in your HTML. Helpers make your templates smarter and more powerful, letting you do things like:

  • Format numbers and dates
  • Loop through arrays with extra options
  • Show or hide content based on conditions
  • Transform strings (capitalize, truncate, etc.)
  • Work with objects or arrays in advanced ways

Helpers can be:

  • Built-in: Provided by the Handlebars library itself (like {{#if}}, {{#each}})
  • Custom: Created by you or your team, for use in your own project
  • ai12z helpers: Powerful out-of-the-box helpers for common use cases (arrays, strings, numbers, objects, etc.), as listed below.

You use helpers in templates like this:

{{capitalize title}}
{{#if (lte price 100)}}On Sale!{{/if}}

And you can create your own helpers in JavaScript:

window.Handlebars.registerHelper("eq", function (a, b) {
return a == b
})

Helpers keep your templates clean, readable, and DRY (Don’t Repeat Yourself), moving complex logic out of your HTML and into easy-to-maintain functions.

Handlebars helper functions

ai12z helpers has out of the box helpers for array, index, number, object, string, regex. Examples are:

HelperWhat it DoesExample Use
lteChecks if a is ≤ b{{#if (lte value 10)}} ... {{/if}}
gteChecks if a is ≥ b{{#if (gte value 5)}} ... {{/if}}
andBlock helper: both arguments must be truthy{{#and a b}} ... {{/and}}
getPageCountReturns total page count from context{{getPageCount}}
getcurrentPageReturns current page from context{{getcurrentPage}}
processArrayOutputs array as a safe JSON string{{processArray items}}
jsonOutputs context as a JSON string, escapes for HTML{{json data}}

Here’s a quick summary table of your arrayHelpers for Handlebars, showing the helper name, what it does, and a short usage example:

Helper NameDescriptionExample Usage
afterReturns items after the given index{{after array 1}}
arrayifyCasts value to an array{{arrayify "foo"}}
beforeReturns all items before the last n{{before array 2}}
eachIndexLoops array, exposes item & index in block{{#eachIndex array}} ... {{/eachIndex}}
filterFilters array by value or property, block helper{{#filter array "foo"}}...{{else}}...{{/filter}}
firstReturns first item or first n items{{first array 2}}
inArrayBlock helper: true if array contains value{{#inArray array "d"}}...{{else}}...{{/inArray}}
isArrayReturns true if value is an array{{isArray array}}
itemAtReturns item at index{{itemAt array 1}}
joinJoins array into string with separator{{join array "-"}}
equalsLengthBlock: true if value has given length{{#equalsLength array 3}} ... {{/equalsLength}}
lastReturns last item or last n items{{last array 2}}
lengthReturns length of array, object, or string{{length array}}
mapMaps array with iterator function{{map array fn}}
pluckReturns array of property values{{pluck array "prop"}}
reverseReverses array or string{{reverse array}}
someBlock: true if any item passes function{{#some array fn}}...{{/some}}
sortSorts array, optionally reverse{{sort array}}
sortBySorts array of objects by property{{sortBy array "prop"}}
withAfterBlock: context is items after index{{#withAfter array 2}}...{{/withAfter}}
withBeforeBlock: context is items before index{{#withBefore array 2}}...{{/withBefore}}
withFirstBlock: context is first item or first n{{#withFirst array 2}}...{{/withFirst}}
withGroupBlock: groups array elements by size{{#withGroup array 3}}...{{/withGroup}}
withLastBlock: context is last item or last n{{#withLast array 2}}...{{/withLast}}
withSortBlock: context is sorted array (by property, optional reverse){{#withSort array "prop"}}...{{/withSort}}
uniqueReturns array with duplicates removed{{unique array}}

Here’s a summary table for your numberHelpers object, showing the helper name, what it does, and a short usage example:

Helper NameDescriptionExample Usage
bytesFormats a number or string as bytes (B, kB, MB, etc.){{bytes 1396}}'1.4 kB'
addCommasAdds commas as thousands separators{{addCommas 1234567}}'1,234,567'
phoneNumberFormats number as US phone number{{phoneNumber 8005551212}}'(800) 555-1212'
toAbbrAbbreviates number to k, m, b, t, or q (thousands/millions, etc.){{toAbbr 10000}}'10k'
toExponentialConverts number to exponential notation{{toExponential 12345 2}}'1.23e+4'
toFixedFormats number using fixed-point notation{{toFixed 1.1234 2}}'1.12'
toFloatConverts value to float{{toFloat "3.14"}}3.14
toIntConverts value to integer{{toInt "42"}}42
toPrecisionFormats number to specified precision (significant digits){{toPrecision 1.1234 2}}'1.1'

Here’s a summary table for your objectHelpers object, showing the helper name, what it does, and a short usage example:

Helper NameDescriptionExample Usage
extendShallow-merge one or more objects into a new object{{extend obj1 obj2}}
toPathConverts arguments to a dot-separated property path string{{toPath "user" "name"}}'user.name'
getGets value at given property path (dot notation supported){{get "user.name" context}}
getObjectGets nested object at path (returns object, not just value){{getObject "a.b" context}}
hasOwnTrue if key is an own property of the object{{hasOwn context "foo"}}
JSONparseParses a string as JSON{{JSONparse '{"foo":"bar"}'}}{ foo: "bar" }
JSONstringifyConverts object to JSON string (with optional indentation){{JSONstringify object 2}}
mergeDeep merges all arguments into the first object{{merge obj1 obj2 obj3}}
stringifyAlias for JSONstringify{{stringify object 2}}

Here’s a summary table for your stringHelpers object, showing the helper name, what it does, and a short usage example:

Helper NameDescriptionExample Usage
appendAppends a suffix to a string{{append "foo" ".html"}}'foo.html'
camelcaseConverts string to camelCase{{camelcase "foo bar baz"}}'fooBarBaz'
capitalizeCapitalizes first character{{capitalize "foo bar"}}'Foo bar'
capitalizeAllCapitalizes every word{{capitalizeAll "foo bar"}}'Foo Bar'
centerPads string with non-breaking spaces{{center "foo" 2}}
chopTrims whitespace and non-word chars from ends{{chop "_ABC_"}}'ABC'
dashcaseConverts to dash-case{{dashcase "foo bar"}}'foo-bar'
dotcaseConverts to dot.case{{dotcase "foo bar"}}'foo.bar'
downcaseConverts to lowercase (alias for lowercase){{downcase "FOO"}}'foo'
ellipsisTruncates and adds ellipsis{{ellipsis "foo bar baz" 7}}'foo bar…'
hyphenateReplaces spaces with hyphens{{hyphenate "foo bar"}}'foo-bar'
isStringReturns true if value is a string{{isString "foo"}}true
lowercaseConverts to lowercase{{lowercase "Foo"}}'foo'
occurrencesCounts substring occurrences{{occurrences "foo bar foo" "foo"}}2
pascalcaseConverts to PascalCase{{pascalcase "foo bar"}}'FooBar'
pathcaseConverts to path/case{{pathcase "foo bar"}}'foo/bar'
plusifyReplaces spaces (or char) with pluses{{plusify "foo bar"}}'foo+bar'
prependPrepends prefix to string{{prepend "bar" "foo-"}}'foo-bar'
rawOutputs block without parsing Handlebars insideSee docs, block usage
removeRemoves all occurrences of substring{{remove "a b a" "a"}}' b '
removeFirstRemoves first occurrence of substring{{removeFirst "a b a" "a"}}' b a'
replaceReplaces all occurrences of substring{{replace "a b a" "a" "z"}}'z b z'
replaceFirstReplaces first occurrence of substring{{replaceFirst "a b a" "a" "z"}}'z b a'
reverseReverses the string{{reverse "abc"}}'cba'
sentenceSentence-cases the string{{sentence "foo. bar."}}'Foo. Bar.'
snakecaseConverts to snake_case{{snakecase "foo bar"}}'foo_bar'
splitSplits string into array{{split "a,b,c" ","}}['a','b','c']
startsWithBlock: true if string starts with prefix{{#startsWith "foo" "foobar"}} ... {{/startsWith}}
titleizeConverts to Title Case{{titleize "foo bar"}}'Foo Bar'
trimTrims whitespace from both ends{{trim " foo "}}'foo'
trimLeftTrims whitespace from left{{trimLeft " foo "}}'foo '
trimRightTrims whitespace from right{{trimRight " foo "}}' foo'
truncateTruncates string to length, adds optional suffix{{truncate "foobar" 3}}'foo'
truncateWordsTruncates to N words, adds optional suffix{{truncateWords "foo bar baz" 2}}'foo bar…'
upcaseConverts to uppercase (alias for uppercase){{upcase "foo"}}'FOO'
uppercaseConverts to uppercase{{uppercase "foo"}}'FOO'

Creating Custom handlebars helpers

In Web Controls -> ai12z-bot -> edit a config, then in the Script tab you can add your own handlebars helpers

Example below,

window.Handlebars.registerHelper('eq', function(a, b) { return a == b; });