Mockoon templating helpers


In addition to Handlebars' built-in helpers, Mockoon offers the following helpers:

Block helpersArraysMath
repeatarrayaddeq
switchoneOfsubtractgt
someOfmultiplygte
joindividelt
slicemodulolte
lenceiltoFixed
floorround
StringsDatesMisc
intsplitnownewline
floatstringifydateTimeShiftbase64
includesconcatdateobjectId
substrindexOftimesetVar
Faker.js aliases
booleanstreetphone
titlecitycolor
firstNamecountryhexColor
lastNamecountryCodeguid
companyzipcodeipv4
domainpostcodeipv6
tldlatlorem
emaillong

repeat

Repeat the block content a random number of times if two arguments are provided, or a fixed amount of time if only one argument is provided. Set the comma parameter to false (default to true) to prevent the insertion of new lines and commas by the helper.

Parameters/argumentsTypeDescription
[0]numberMinimum items
1numberMaximum items
[comma=true]booleanAdd trailing comma

Examples

{{#repeat 5 10 comma=true}}test{{/repeat}} <!-- result: test, test, test, test, test -->

switch

Select some content depending on a variable. Behaves like a regular switch.

Arguments (ordered)TypeDescription
0anyValue against which the switch matches the cases

Examples

{{# switch (urlParam 'id')}} {{# case "1" }}"John"{{/ case }} {{# case "2" }}"Jack"{{/ case }} {{# default }}"Peter"{{/ default }} {{# /}}

array

Create an array from the given items. This helper is mostly used with the following helpers: oneOf, someOf.

Arguments (ordered)TypeDescription
0..nanyValue used to populate the array

Examples

{{array 'item1' 'item2' 'item3'}}

oneOf

Select a random item in the array passed in parameters.

Arguments (ordered)TypeDescription
0any[]Array of values

Examples

{{oneOf (array 'item1' 'item2' 'item3')}} result: item2

someOf

Return a random number of items from the array passed in parameters, concatenated as a string. Use it with triple curly braces to get a JSON representation.

Arguments (ordered)TypeDescription
0any[]Array of values
1numberMinimum number of items
2numberMaximum number of items
[3 = false]booleanGet result as an array

Examples

{{someOf (array 'item1' 'item2' 'item3') 1 2}} result: item1,item2 <!-- Use triple curly braces to avoid character escaping --> {{{someOf (array 'item1' 'item2' 'item3') x y true}}} result: item1,item2

join

Return a new string by concatenating all the elements in an array.

Arguments (ordered)TypeDescription
0[]Array of values
1stringSeparator

Examples

{{join (array 'item1' 'item2' 'item3') '#'}} result: item1#item2#item3

slice

Return a copy of a portion of an array from start to end indexes (not included).

Arguments (ordered)TypeDescription
0[]Array of values
1numberStart index
2numberEnd index

Examples

{{slice (array 'item1' 'item2' 'item3') 0 2}} result: ['item1', 'item2']

len

Return an array or string length.

Arguments (ordered)TypeDescription
0[] | stringArray or string

Examples

{{len (array 'item1' 'item2' 'item3')}} result: 3 {{len 'hello'}} result: 5

add

Add the numbers passed as parameters to each others. Unrecognized strings passed as arguments will be ignored.

Arguments (ordered)TypeDescription
0..nany[]Value of the operandes (can process numbers and strings)

Examples

{{add 1 1}} result: '2' {{add '1' '1'}} result: '2' {{add '1' 'foo' 1}} result: '2'

subtract

Subtract the numbers passed as parameters to the first parameter. Unrecognized strings passed as arguments will be ignored.

Arguments (ordered)TypeDescription
0..nany[]Value of the operandes (can process numbers and strings)

Examples

{{subtract 2 1}} result: '1' {{subtract '2' '1'}} result: '1' {{subtract '2' 'foo' 1}} result: '1'

multiply

Multiply the numbers passed as parameters to each others. Unrecognized strings passed as arguments will be ignored.

Arguments (ordered)TypeDescription
0..nany[]Value of the operandes (can process numbers and strings)

Examples

{{multiply 2 3}} result: '6' {{multiply '2' '3'}} result: '6' {{multiply '2' 'foo' 3}} result: '6'

divide

Divide the first parameter by the other numbers passed as parameters. Unrecognized strings passed as arguments will be ignored.

Arguments (ordered)TypeDescription
0..nany[]Value of the operandes (can process numbers and strings)

Examples

{{divide 4 2}} result: '2' {{divide '4' '2'}} result: '2' {{divide '4' 'foo' 2}} result: '2'

modulo

Compute the modulo of the first parameter by the second.

Arguments (ordered)TypeDescription
0numberFirst value (can process numbers and strings)
1numberSecond value (can process numbers and strings)

Examples

{{modulo 5 4}} result: '1' {{modulo '5' '4'}} result: '1' {{modulo '5' 'foo' 4}} result: '1'

ceil

Ceil the value of the number passed as parameter.

Arguments (ordered)TypeDescription
0numberValue to ceil (can process numbers and strings)

Examples

{{ceil 1.5}} result: '2' {{ceil '1.5'}} result: '2'

floor

Floor the value of the number passed as parameter.

Arguments (ordered)TypeDescription
0numberValue to floor (can process numbers and strings)

Examples

{{floor 2.5}} result: '2' {{floor '2.5'}} result: '2'

eq

Verify if two numbers are equal. Returns a boolean.

Arguments (ordered)TypeDescription
0numberFirst number
1numberSecond number

Examples

{{#if (eq 55 55}} true {{/if}} Result: true

gt

Verify if the first number is greater than the second number. Returns a boolean.

Arguments (ordered)TypeDescription
0numberFirst number
1numberSecond number

Examples

{{#if (gt 56 55)}} true {{/if}} Result: true

gte

Verify if the first number is greater than or equal to the second number. Returns a boolean.

Arguments (ordered)TypeDescription
0numberFirst number
1numberSecond number

Examples

{{#if (gte 55 55)}} true {{/if}} Result: true

lt

Verify if the first number is lower than the second number. Returns a boolean.

Arguments (ordered)TypeDescription
0numberFirst number
1numberSecond number

Examples

{{#if (lt 55 56)}} true {{/if}} Result: true

lte

Verify if the first number is lower than or equal to the second number. Returns a boolean.

Arguments (ordered)TypeDescription
0numberFirst number
1numberSecond number

Examples

{{#if (lte 55 55)}} true {{/if}} Result: true

toFixed

Format a number using fixed-point notation.

Arguments (ordered)TypeDescription
0numberA number
1numberNumber of digits

Examples

{{toFixed 1.11111 2}} Result: 1.11

round

Return the value of a number rounded to the nearest integer.

Arguments (ordered)TypeDescription
0numberA number to round

Examples

{{round 0.499}} Result: 0

newline

Add a newline \n.

Examples

{{newline}}

base64

Encode the parameter as base64. This can be used as an inline helper or block helper (see examples below).

Arguments (ordered)TypeDescription
[0]anyValue to encode (optional when used as a block helper)

Examples

{{base64 'test'}} {{#base64}} firstname,lastname,countryCode {{#repeat 10}} {{faker 'name.firstName'}},{{faker 'name.lastName'}},{{faker 'address.countryCode' }} {{/repeat}} {{/base64}}

objectId

Create a valid ObjectId. It can generates the ObjectId based on the specified time (in seconds) or from a 12 byte string that will act as a seed. Syntax is based on bson-objectid package.

Arguments (ordered)TypeDescription
0string | numberSeed

Examples

{{objectId 1414093117}} {{objectId '54495ad94c934721ede76d90'}}

setVar

Set a variable to be used later in the template. The value can be the result of another helper. To use it elsewhere in the template, refer to the variable with its name: {{varname}} or {{@root.varname}}. The variable can also be used as a helper parameter: {{#repeat varname}}...{{/repeat}}. You can also declare a variable inside block helpers like repeat. Accessing such variables requires to prefix them with @ (see examples below).

Arguments (ordered)TypeDescription
0stringVariable name
1anyVariable value

Examples

{{setVar 'varname' 'value'}} {{setVar 'varname' (body 'id')}} usage: {{varname}} {{@root.varname}} {{#repeat varname}}...{{/repeat}} declare a variable in a block helper: {{#repeat 5}} {{setVar 'random' (oneOf (array '1' '2' '3'))}} {{@random}} {{/repeat}}

int

Return a random integer. Alias of faker 'random.number.

Arguments (ordered)TypeDescription
0numberMinimum
1numberMaximum

Examples

{{int 0 100}}

float

Return a random float. Alias of faker 'random.number with precision = 10.

Arguments (ordered)TypeDescription
0numberMinimum
1numberMaximum

Examples

{{float 0 100}}

includes

Search whether a string can be found in another string and returns the appropriate boolean.

Arguments (ordered)TypeDescription
0anyData to search into
1anyData to search

Examples

{{includes 'Some data' 'data'}} result: true

substr

Return a portion of a string starting at the specified index and extending for a given number of characters afterwards.

Arguments (ordered)TypeDescription
0anyStarting index
[1 = max length]anyLength

Examples

{{substr 'Some data' 5 4}} result: 'data'

split

Split a string and return an array containing the multiples substrings. This helper can be used within handlebars' iterative helpers such as each. (Default separator is " ")

Arguments (ordered)TypeDescription
0stringData to split
1stringSeparator

Examples

{{#each (split '1 2 3 4')}} item{{this}}, {{/each}} result: item1,item2,item3,item4 {{#each (split 'This is my string.')}} {{this}}, {{/each}} result: This,is,my,string,

stringify

Return objects and arrays as a formatted JSON string indented with two spaces.

Arguments (ordered)TypeDescription
0anyObject or array

Examples

Considering an entering body:

{ "prop1": "123", "prop2": { "data": "test" } }
{{{stringify (bodyRaw 'prop2')}}}
{ "data": "test" }

concat

Concatenate multiple strings/numbers together. This helper can concatenate results from other helpers, or be used as a parameter of another helper (see examples below).

Arguments (ordered)TypeDescription
0..nanyValues to concatenate

Examples

{{concat 'value1' 2 'value3'}} {{concat @index (body 'id') 'value3'}} {{#repeat (concat 1 2 3)}}...{{/repeat}}

indexOf

Return the index of the searched 'data' inside the string. A last parameter (number) can be passed to start the search at a specific index.

Arguments (ordered)TypeDescription
0anyData to search into
1anyData to search
[2 = 0]numberSearch starting index

Examples

{{indexOf 'Some data' 'data' 0}} result: 5

now

Display the current time in the chosen format. Format syntax is based on date-fns package (v2) and is optional (default to ISO string).

Arguments (ordered)TypeDescription
0stringDate format

Examples

{{now 'YYYY-MM-DD'}}

dateTimeShift

Shift a date by adding the number of years, months, etc. passed as parameters. The date and format parameters are optional. The helper will return the current date and time as an ISO string if omitted (yyyy-MM-ddTHH:mm:ss.SSSxxx).

Parameters (named)TypeDescription
datestringDate to shift
[format = 'yyyy-MM-ddTHH:mm:ss.SSSxxx']stringFormat of the shifted date
[years = 0]numberYears to shift
[months = 0]numberMonths to shift
[days = 0]numberDays to shift
[hours = 0]numberHours to shift
[minutes = 0]numberMinutes to shift
[seconds = 0]numberSeconds to shift

Examples

{{dateTimeShift date='2021-01-01' format='yyyy-MM-dd HH:mm:ss' years=1 months=1 days=1 hours=1 minutes=1 seconds=1 }}

date

Return a random formatted (using date-fns package format) date between a minimum and a maximum. Uses faker 'date.between' to generate the random date.

Arguments (ordered)TypeDescription
0numberStarting date
1numberEnding date
2stringDate format

Examples

{{date '2020-11-20' '2020-11-25' "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"}}

time

Return a random formatted (using date-fns package format) time between a minimum and a maximum. Uses faker 'date.between' to generate the random time.

Arguments (ordered)TypeDescription
0numberStarting time
1numberEnding time
2stringTime format

Examples

{{time '09:00' '10:00' 'HH:mm'}}

boolean

Return a random boolean. Alias of faker 'random.boolean'.

Examples

{{boolean}}

title

Return a random title. Alias of faker 'name.title'.

Examples:

{{title}}

firstName

Return a random first name. Alias of faker 'name.firstName'.

Examples:

{{firstName}}

lastName

Return a random last name. Alias of faker 'name.lastName'.

Examples:

{{lastName}}

company

Return a random company name. Alias of faker 'company.companyName'.

Examples:

{{company}}

domain

Return a random domain name. Alias of faker 'internet.domainName'.

Examples:

{{domain}}

tld

Return a random top level domain. Alias of faker 'internet.domainSuffix'.

Examples:

{{tld}}

email

Return a random email address. Alias of faker 'internet.email'.

Examples:

{{email}}

street

Return a random address. Alias of faker 'address.streetAddress'.

Examples:

{{street}}

city

Return a random city name. Alias of faker 'address.city'.

Examples:

{{city}}

country

Return a random country name. Alias of faker 'address.country'.

Examples:

{{country}}

countryCode

Return a random country code. Alias of faker 'address.countryCode'.

Examples:

{{countryCode}}

zipcode

Return a random zip code. Alias of faker 'address.zipCode'.

Examples:

{{zipcode}}

postcode

Return a random zip code. Alias of faker 'address.zipCode'.

Examples:

{{postcode}}

lat

Return a random latitude. Alias of faker 'address.latitude'.

Examples:

{{lat}}

long

Return a random longitude. Alias of faker 'address.longitude'.

Examples:

{{long}}

phone

Return a random phone number. Alias of faker 'phone.phoneNumber'.

Examples:

{{phone}}

color

Return a random color. Alias of faker 'commerce.color'.

Examples:

{{color}}

hexColor

Return a random hexadecimal color code.

Examples:

{{hexColor}}

guid

Return a random GUID. Alias of faker 'random.uuid'.

Examples:

{{guid}}

ipv4

Return a random IP v4. Alias of faker 'internet.ip'.

Examples:

{{ipv4}}

ipv6

Return a random IP v6. Alias of faker 'internet.ipv6'.

Examples:

{{ipv6}}

lorem

Return random lorem ipsum text. Alias of faker 'lorem.sentence'.

Arguments (ordered)TypeDescription
0numberNumber of words

Examples:

{{lorem 50}}