Mockoon offers the following helpers which can help you set local or global variables, and access environment variables in your templates:
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:
Dynamically get a variable set with setVar
.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Variable name |
Examples
Copy
Set a global variable to be used anywhere templating is supported (body, headers, etc.). Global variables are available on all the routes of an environment and they are reset when the environment is restarted.
{{getGlobalVar 'varName'}}
helper below.Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Variable name |
1 | any | Variable value |
Examples
Copy
Get a global variable's value set with setGlobalVar
. Global variables are available on all the routes of an environment and they are reset when the environment is restarted.
path
supports two syntaxes, object-path or JSONPath Plus. When using object-path, properties containing dots are supported by escaping the dots: key.key\.with\.dot
.path
is omitted ({{getGlobalVar 'varname'}}
).🛠️ Use our online JSONPath and object-path evaluator to test your JSONPath or object-path syntaxes and view the results in real-time.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Variable name |
1 | string | Path to the value property |
Examples
Copy<!-- Using object-path syntax --> <!-- using JSONPath syntax --> ... ... <!-- Stringify the variable content -->
Get an environment variable. If the environment variable is not set, the default value will be used.
By default, only environment variables with the MOCKOON_
prefix are available. Learn more in the environment variables documentation.
Arguments (ordered) | Type | Description |
---|---|---|
0 | string | Environment variable name |
1 | any | Default value |
Examples
Copy