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:
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:
Helper | What it Does | Example Use |
---|---|---|
lte | Checks if a is ≤ b | {{#if (lte value 10)}} ... {{/if}} |
gte | Checks if a is ≥ b | {{#if (gte value 5)}} ... {{/if}} |
and | Block helper: both arguments must be truthy | {{#and a b}} ... {{/and}} |
getPageCount | Returns total page count from context | {{getPageCount}} |
getcurrentPage | Returns current page from context | {{getcurrentPage}} |
processArray | Outputs array as a safe JSON string | {{processArray items}} |
json | Outputs 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 Name | Description | Example Usage |
---|---|---|
after | Returns items after the given index | {{after array 1}} |
arrayify | Casts value to an array | {{arrayify "foo"}} |
before | Returns all items before the last n | {{before array 2}} |
eachIndex | Loops array, exposes item & index in block | {{#eachIndex array}} ... {{/eachIndex}} |
filter | Filters array by value or property, block helper | {{#filter array "foo"}}...{{else}}...{{/filter}} |
first | Returns first item or first n items | {{first array 2}} |
inArray | Block helper: true if array contains value | {{#inArray array "d"}}...{{else}}...{{/inArray}} |
isArray | Returns true if value is an array | {{isArray array}} |
itemAt | Returns item at index | {{itemAt array 1}} |
join | Joins array into string with separator | {{join array "-"}} |
equalsLength | Block: true if value has given length | {{#equalsLength array 3}} ... {{/equalsLength}} |
last | Returns last item or last n items | {{last array 2}} |
length | Returns length of array, object, or string | {{length array}} |
map | Maps array with iterator function | {{map array fn}} |
pluck | Returns array of property values | {{pluck array "prop"}} |
reverse | Reverses array or string | {{reverse array}} |
some | Block: true if any item passes function | {{#some array fn}}...{{/some}} |
sort | Sorts array, optionally reverse | {{sort array}} |
sortBy | Sorts array of objects by property | {{sortBy array "prop"}} |
withAfter | Block: context is items after index | {{#withAfter array 2}}...{{/withAfter}} |
withBefore | Block: context is items before index | {{#withBefore array 2}}...{{/withBefore}} |
withFirst | Block: context is first item or first n | {{#withFirst array 2}}...{{/withFirst}} |
withGroup | Block: groups array elements by size | {{#withGroup array 3}}...{{/withGroup}} |
withLast | Block: context is last item or last n | {{#withLast array 2}}...{{/withLast}} |
withSort | Block: context is sorted array (by property, optional reverse) | {{#withSort array "prop"}}...{{/withSort}} |
unique | Returns 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 Name | Description | Example Usage |
---|---|---|
bytes | Formats a number or string as bytes (B, kB, MB, etc.) | {{bytes 1396}} → '1.4 kB' |
addCommas | Adds commas as thousands separators | {{addCommas 1234567}} → '1,234,567' |
phoneNumber | Formats number as US phone number | {{phoneNumber 8005551212}} → '(800) 555-1212' |
toAbbr | Abbreviates number to k, m, b, t, or q (thousands/millions, etc.) | {{toAbbr 10000}} → '10k' |
toExponential | Converts number to exponential notation | {{toExponential 12345 2}} → '1.23e+4' |
toFixed | Formats number using fixed-point notation | {{toFixed 1.1234 2}} → '1.12' |
toFloat | Converts value to float | {{toFloat "3.14"}} → 3.14 |
toInt | Converts value to integer | {{toInt "42"}} → 42 |
toPrecision | Formats 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 Name | Description | Example Usage |
---|---|---|
extend | Shallow-merge one or more objects into a new object | {{extend obj1 obj2}} |
toPath | Converts arguments to a dot-separated property path string | {{toPath "user" "name"}} → 'user.name' |
get | Gets value at given property path (dot notation supported) | {{get "user.name" context}} |
getObject | Gets nested object at path (returns object, not just value) | {{getObject "a.b" context}} |
hasOwn | True if key is an own property of the object | {{hasOwn context "foo"}} |
JSONparse | Parses a string as JSON | {{JSONparse '{"foo":"bar"}'}} → { foo: "bar" } |
JSONstringify | Converts object to JSON string (with optional indentation) | {{JSONstringify object 2}} |
merge | Deep merges all arguments into the first object | {{merge obj1 obj2 obj3}} |
stringify | Alias 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 Name | Description | Example Usage |
---|---|---|
append | Appends a suffix to a string | {{append "foo" ".html"}} → 'foo.html' |
camelcase | Converts string to camelCase | {{camelcase "foo bar baz"}} → 'fooBarBaz' |
capitalize | Capitalizes first character | {{capitalize "foo bar"}} → 'Foo bar' |
capitalizeAll | Capitalizes every word | {{capitalizeAll "foo bar"}} → 'Foo Bar' |
center | Pads string with non-breaking spaces | {{center "foo" 2}} |
chop | Trims whitespace and non-word chars from ends | {{chop "_ABC_"}} → 'ABC' |
dashcase | Converts to dash-case | {{dashcase "foo bar"}} → 'foo-bar' |
dotcase | Converts to dot.case | {{dotcase "foo bar"}} → 'foo.bar' |
downcase | Converts to lowercase (alias for lowercase ) | {{downcase "FOO"}} → 'foo' |
ellipsis | Truncates and adds ellipsis | {{ellipsis "foo bar baz" 7}} → 'foo bar…' |
hyphenate | Replaces spaces with hyphens | {{hyphenate "foo bar"}} → 'foo-bar' |
isString | Returns true if value is a string | {{isString "foo"}} → true |
lowercase | Converts to lowercase | {{lowercase "Foo"}} → 'foo' |
occurrences | Counts substring occurrences | {{occurrences "foo bar foo" "foo"}} → 2 |
pascalcase | Converts to PascalCase | {{pascalcase "foo bar"}} → 'FooBar' |
pathcase | Converts to path/case | {{pathcase "foo bar"}} → 'foo/bar' |
plusify | Replaces spaces (or char) with pluses | {{plusify "foo bar"}} → 'foo+bar' |
prepend | Prepends prefix to string | {{prepend "bar" "foo-"}} → 'foo-bar' |
raw | Outputs block without parsing Handlebars inside | See docs, block usage |
remove | Removes all occurrences of substring | {{remove "a b a" "a"}} → ' b ' |
removeFirst | Removes first occurrence of substring | {{removeFirst "a b a" "a"}} → ' b a' |
replace | Replaces all occurrences of substring | {{replace "a b a" "a" "z"}} → 'z b z' |
replaceFirst | Replaces first occurrence of substring | {{replaceFirst "a b a" "a" "z"}} → 'z b a' |
reverse | Reverses the string | {{reverse "abc"}} → 'cba' |
sentence | Sentence-cases the string | {{sentence "foo. bar."}} → 'Foo. Bar.' |
snakecase | Converts to snake_case | {{snakecase "foo bar"}} → 'foo_bar' |
split | Splits string into array | {{split "a,b,c" ","}} → ['a','b','c'] |
startsWith | Block: true if string starts with prefix | {{#startsWith "foo" "foobar"}} ... {{/startsWith}} |
titleize | Converts to Title Case | {{titleize "foo bar"}} → 'Foo Bar' |
trim | Trims whitespace from both ends | {{trim " foo "}} → 'foo' |
trimLeft | Trims whitespace from left | {{trimLeft " foo "}} → 'foo ' |
trimRight | Trims whitespace from right | {{trimRight " foo "}} → ' foo' |
truncate | Truncates string to length, adds optional suffix | {{truncate "foobar" 3}} → 'foo' |
truncateWords | Truncates to N words, adds optional suffix | {{truncateWords "foo bar baz" 2}} → 'foo bar…' |
upcase | Converts to uppercase (alias for uppercase ) | {{upcase "foo"}} → 'FOO' |
uppercase | Converts 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; });