In addition to Handlebars' built-in helpers, Mockoon offers the following helpers:
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/arguments | Type | Description |
---|---|---|
[0] | number | Minimum items |
1 | number | Maximum items |
[comma=true] | boolean | Add trailing comma |
Examples
Copytest <!-- result: test, test, test, test, test -->
switch
#Select some content depending on a variable. Behaves like a regular switch.
Arguments (ordered) | Type | Description |
---|---|---|
0 | any | Value against which the switch matches the cases |
Examples
Copy"John" "Jack" "Peter"
data
#Get the stringified value at a given path
from a data bucket selected by ID or name. This helper is designed to retrieve data to be served in a response. To reuse the retrieved data with other helpers (each
, if
, etc.), use the dataRaw
helper below.
path
takes the following form key.0.key.5.key
and is based on the object-path library. Properties containing dots are supported by escaping the dots: key.key\.with\.dot
.path
is omitted ({{data 'ID'}}
).Arguments (ordered) | Type | Description |
---|---|---|
0 | string | ID or name of the daa bucket |
1 | string | Path to the data bucket property |
Examples
Copy
dataRaw
#Get the raw value (array, object, etc.) at a given path
from a data bucket selected by ID or name. This "raw" helper is designed to work with other helpers (each
, if
, etc.). To directly use the retrieved data in the response, use data buckets direct linking or the data
helper above.
path
takes the following form key.0.key.5.key
and is based on the object-path library. Properties containing dots are supported by escaping the dots: key.key\.with\.dot
.path
is omitted ({{dataRaw 'ID'}}
).Arguments (ordered) | Type | Description |
---|---|---|
0 | string | ID or name of the daa bucket |
1 | string | Path to the data bucket property |
Examples
Copyvalue value
array
#Create an array from the given items. This helper is mostly used with the following helpers: oneOf
, someOf
.
Arguments (ordered) | Type | Description |
---|---|---|
0..n | any | Value used to populate the array |
Examples
Copy
oneOf
#Select a random item in the array passed in parameters. oneOf
will return the actual value in the array. Set the stringify
parameter to true
(default to false
) to get a JSON stringified result.
Arguments (ordered) | Type | Description |
---|---|---|
0 | any[] | Array of values |
[1 = false] | boolean | Stringify the result |
Examples
Copyresult: 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) | Type | Description |
---|---|---|
0 | any[] | Array of values |
1 | number | Minimum number of items |
2 | number | Maximum number of items |
[3 = false] | boolean | Get result as an array |
Examples
Copyresult: item1,item2 <!-- Use triple curly braces to avoid character escaping --> result: item1,item2
join
#Return a new string by concatenating all the elements in an array.
Arguments (ordered) | Type | Description |
---|---|---|
0 | [] | Array of values |
1 | string | Separator |
Examples
Copyresult: item1#item2#item3
slice
#Return a copy of a portion of an array from start to end indexes (not included).
Arguments (ordered) | Type | Description |
---|---|---|
0 | [] | Array of values |
1 | number | Start index |
2 | number | End index |
Examples
Copyresult: ['item1', 'item2']
len
#Return an array or string length.
Arguments (ordered) | Type | Description |
---|---|---|
0 | [] | string | Array or string |
Examples
Copyresult: 3 result: 5
filter
#Return a filtered array. This helper can be used with data buckets, use the dataRaw for that.
Arguments (ordered) | Type | Description |
---|---|---|
0 | any[] | Input array |
1..n | primitive or object or array | OR conditions level |
filter (array 1 2 3) 1 3 5 6 ....
.
AND
.filter (array 1 2 3) 1 3
equals fo [1,2,3].filter(x => x === 3 || x === 1)
.AND
sub-conditions list.OR
sub-conditions list.OR
(the first level of the arguments) -> AND
-> OR
-> AND
-> ...AND
queries with objects please check the object helper documentation.Structure
Copy<!-- filter query base OR structure --> result: c1 OR c2 OR c3 <!-- filter query base AND structure (the AND conditions described as sub-conditions) --> result: items that fit to c1 AND c2 AND c3 <!-- filter query a few OR from several AND conditions --> result: (a1 AND a2 AND a3) OR (b1 AND b2 AND b3) OR (c1 AND c2 AND c3) <!-- filter with complex nested structure --> results: items that fit to (a1 AND a2 AND (x1 OR x2) ) OR (b1 AND b2 AND b3)
Examples
Copy<!-- Simple OR filter --> result: 1,3 <!-- Simple OR filter --> result: item1,item2,item3,item3 <!-- Data bucket get all users with type='item1' --> <!-- Data bucket get all users with type='item1' OR type='item2' OR type='item3' --> <!-- Data bucket get all users with type='item1' AND category='some-category' --> <!-- Data bucket get all users with type='item1' OR category='some-category' --> <!-- Mixed data filter -->
object
#Return an object that can be used in other helpers.
Parameters (named) | Type | Description |
---|---|---|
[string]=any | any | key=value notation of the object properties |
Examples
Copyresult: {type: 'item1'} result: {type: 'item1', category: 'cat1'} result: {type: [1,2,3]} result: {type: [1,2,3]} result: {type: [1,3]}
add
#Add the numbers passed as parameters to each others. Unrecognized strings passed as arguments will be ignored.
Arguments (ordered) | Type | Description |
---|---|---|
0..n | any[] | Value of the operandes (can process numbers and strings) |
Examples
Copyresult: '2' result: '2' result: '2'
subtract
#Subtract the numbers passed as parameters to the first parameter. Unrecognized strings passed as arguments will be ignored.
Arguments (ordered) | Type | Description |
---|---|---|
0..n | any[] | Value of the operandes (can process numbers and strings) |
Examples
Copyresult: '1' result: '1' result: '1'
multiply
#Multiply the numbers passed as parameters to each others. Unrecognized strings passed as arguments will be ignored.
Arguments (ordered) | Type | Description |
---|---|---|
0..n | any[] | Value of the operandes (can process numbers and strings) |
Examples
Copyresult: '6' result: '6' result: '6'
divide
#Divide the first parameter by the other numbers passed as parameters. Unrecognized strings passed as arguments will be ignored.
Arguments (ordered) | Type | Description |
---|---|---|
0..n | any[] | Value of the operandes (can process numbers and strings) |
Examples
Copyresult: '2' result: '2' result: '2'
modulo
#Compute the modulo of the first parameter by the second.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | First value (can process numbers and strings) |
1 | number | Second value (can process numbers and strings) |
Examples
Copyresult: '1' result: '1' result: '1'
ceil
#Ceil the value of the number passed as parameter.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | Value to ceil (can process numbers and strings) |
Examples
Copyresult: '2' result: '2'
floor
#Floor the value of the number passed as parameter.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | Value to floor (can process numbers and strings) |
Examples
Copyresult: '2' result: '2'
eq
#Verify if two numbers or strings are equal. Returns a boolean.
Returns false if type of the value not equals.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | number | First number or string |
1 | string | number | Second number or string |
Examples
Copytrue Result: true true false Result: false true Result: true
gt
#Verify if the first number is greater than the second number. Returns a boolean.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | First number |
1 | number | Second number |
Examples
Copytrue Result: true
gte
#Verify if the first number is greater than or equal to the second number. Returns a boolean.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | First number |
1 | number | Second number |
Examples
Copytrue Result: true
lt
#Verify if the first number is lower than the second number. Returns a boolean.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | First number |
1 | number | Second number |
Examples
Copytrue Result: true
lte
#Verify if the first number is lower than or equal to the second number. Returns a boolean.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | First number |
1 | number | Second number |
Examples
Copytrue Result: true
toFixed
#Format a number using fixed-point notation.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | A number |
1 | number | Number of digits |
Examples
CopyResult: 1.11
round
#Return the value of a number rounded to the nearest integer.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | A number to round |
Examples
CopyResult: 0
newline
#Add a newline \n
.
Examples
Copy
base64
#Encode the parameter as base64. This can be used as an inline helper or block helper (see examples below).
Arguments (ordered) | Type | Description |
---|---|---|
[0] | any | Value to encode (optional when used as a block helper) |
Examples
Copyfirstname,lastname,countryCode ,,
base64Decode
#Decode a base64 string. This can be used as an inline helper or block helper (see examples below).
Arguments (ordered) | Type | Description |
---|---|---|
[0] | string | Base64 string to decode (optional when used as a block helper) |
Examples
Copy
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) | Type | Description |
---|---|---|
0 | string | number | Seed |
Examples
Copy
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 prefixed with an @
: {{@varname}}
. The variable can also be used as a helper parameter: {{#repeat @varname}}...{{/repeat}}
.
Variables declared in a block helper will be scoped to the block and unavailable outside.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Variable name |
1 | any | Variable value |
Examples
Copyusage: ... declare a variable in a block helper:
getVar
#Dynamically get a variable set with setVar
.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Variable name |
Examples
Copy
includes
#Search whether a string can be found in another string and returns the appropriate boolean.
Arguments (ordered) | Type | Description |
---|---|---|
0 | any | Data to search into |
1 | any | Data to search |
Examples
Copyresult: true
substr
#Return a portion of a string starting at the specified index and extending for a given number of characters afterwards.
Arguments (ordered) | Type | Description |
---|---|---|
0 | any | Starting index |
[1 = max length] | any | Length |
Examples
Copyresult: 'data'
lowercase
#Return the first string parameter lowercased.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | String to lowercase |
Examples
Copyresult: 'abcd'
uppercase
#Return the first string parameter uppercased.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | String to uppercase |
Examples
Copyresult: 'ABCD'
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) | Type | Description |
---|---|---|
0 | string | Data to split |
1 | string | Separator |
Examples
Copyitem, result: item1,item2,item3,item4 , result: This,is,my,string,
stringify
#Return objects and arrays as a formatted JSON string indented with two spaces.
Arguments (ordered) | Type | Description |
---|---|---|
0 | any | Object or array |
Examples
Considering an entering body:
Copy{ "prop1": "123", "prop2": { "data": "test" } }
Copy
Copy{ "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) | Type | Description |
---|---|---|
0..n | any | Values to concatenate |
Examples
Copy...
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) | Type | Description |
---|---|---|
0 | any | Data to search into |
1 | any | Data to search |
[2 = 0] | number | Search starting index |
Examples
Copyresult: 5
parseInt
#Parse a number from a string.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | String to parse |
padStart
#Pads a string with a given string (repeated, if needed) until the resulting string reaches the given length. The padding is applied from the start of the string.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | String to pad |
1 | number | pad length |
[2 = ' '] | string | Padding character(s) (default to blank space) |
Copyresult: 00005
padEnd
#Pads a string with a given string (repeated, if needed) until the resulting string reaches the given length. The padding is applied from the end of the string.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | String to pad |
1 | number | pad length |
[2 = ' '] | string | Padding character(s) (default to blank space) |
Examples
Copyresult: 50000
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) | Type | Description |
---|---|---|
0 | string | Date format |
Examples
Copy
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) | Type | Description |
---|---|---|
date | string | Date to shift |
[format = 'yyyy-MM-ddTHH:mm:ss.SSSxxx'] | string | Format of the shifted date |
[years = 0] | number | Years to shift |
[months = 0] | number | Months to shift |
[days = 0] | number | Days to shift |
[hours = 0] | number | Hours to shift |
[minutes = 0] | number | Minutes to shift |
[seconds = 0] | number | Seconds to shift |
Examples
Copy
date
#Return a random formatted date (using date-fns package format) between a minimum and a maximum. Uses faker 'date.between'
to generate the random date.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Starting date |
1 | string | Ending date |
2 | string | Date format |
Examples
Copy
time
#Return a random formatted time (using date-fns package format) between a minimum and a maximum. Uses faker 'date.between'
to generate the random time.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | Starting time |
1 | number | Ending time |
2 | string | Time format |
Examples
Copy
dateFormat
#Return a formatted date (using date-fns package format).
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Date | Date to format |
1 | string | Output format |
Examples
Copy
int
#Return a random integer. Alias of faker 'datatype.number
.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | Minimum |
1 | number | Maximum |
Examples
Copy
float
#Return a random float. Alias of faker 'datatype.number
with precision = 10.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | Minimum |
1 | number | Maximum |
Examples
Copy
boolean
#Return a random boolean. Alias of faker 'datatype.boolean'
.
Examples
Copy
title
#Return a random title. Alias of faker 'name.title'
.
Examples:
Copy
firstName
#Return a random first name. Alias of faker 'name.firstName'
.
Examples:
Copy
lastName
#Return a random last name. Alias of faker 'name.lastName'
.
Examples:
Copy
company
#Return a random company name. Alias of faker 'company.companyName'
.
Examples:
Copy
domain
#Return a random domain name. Alias of faker 'internet.domainName'
.
Examples:
Copy
tld
#Return a random top level domain. Alias of faker 'internet.domainSuffix'
.
Examples:
Copy
email
#Return a random email address. Alias of faker 'internet.email'
.
Examples:
Copy
street
#Return a random address. Alias of faker 'address.streetAddress'
.
Examples:
Copy
city
#Return a random city name. Alias of faker 'address.city'
.
Examples:
Copy
country
#Return a random country name. Alias of faker 'address.country'
.
Examples:
Copy
countryCode
#Return a random country code. Alias of faker 'address.countryCode'
.
Examples:
Copy
zipcode
#Return a random zip code. Alias of faker 'address.zipCode'
.
Examples:
Copy
postcode
#Return a random zip code. Alias of faker 'address.zipCode'
.
Examples:
Copy
lat
#Return a random latitude. Alias of faker 'address.latitude'
.
Examples:
Copy
long
#Return a random longitude. Alias of faker 'address.longitude'
.
Examples:
Copy
phone
#Return a random phone number. Alias of faker 'phone.phoneNumber'
.
Examples:
Copy
color
#Return a random color. Alias of faker 'commerce.color'
.
Examples:
Copy
hexColor
#Return a random hexadecimal color code.
Examples:
Copy
guid
#Return a random GUID. Alias of faker 'datatype.uuid'
.
Examples:
Copy
ipv4
#Return a random IP v4. Alias of faker 'internet.ip'
.
Examples:
Copy
ipv6
#Return a random IP v6. Alias of faker 'internet.ipv6'
.
Examples:
Copy
lorem
#Return random lorem ipsum text. Alias of faker 'lorem.sentence'
.
Arguments (ordered) | Type | Description |
---|---|---|
0 | number | Number of words |
Examples:
Copy