Mockoon templating helpers


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

DatesBlock helpersArrays
nowrepeatarray
dateTimeShiftswitchoneOf
datesomeOf
time
StringsNumbersMisc
concatintnewline
indexOffloatbase64
includesobjectId
substrsetVar
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

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

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

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

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

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

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'

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

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