-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add environment variables documentation see mockoon/mockoon#849 Add new docs variables pages Update docs images
- Loading branch information
Showing
244 changed files
with
3,883 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
content/docs/latest/templating/mockoon-variables-helpers.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
--- | ||
title: Variables helpers | ||
meta: | ||
title: Dynamically customize your responses with variables templating helpers | ||
description: "Dynamically customize your mock environments response with Mockoon's templating variables helpers. All formats are supported: JSON, CSV, HTML, etc." | ||
order: 503 | ||
--- | ||
|
||
# Templating variables helpers | ||
|
||
--- | ||
|
||
Mockoon offers the following helpers which can help you set **local or global variables**, and **access environment variables** in your templates: | ||
|
||
- [`setVar`](#setvar) | ||
- [`getVar`](#getvar) | ||
- [`setGlobalVar`](#setglobalvar) | ||
- [`getGlobalVar`](#getglobalvar) | ||
- [`getEnvVar`](#getenvvar) | ||
|
||
## 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** | ||
|
||
```handlebars | ||
{{setVar 'varname' 'value'}} | ||
{{setVar 'varname' (body 'id')}} | ||
usage: | ||
{{@varname}} | ||
{{#repeat @varname}}...{{/repeat}} | ||
declare a variable in a block helper: | ||
{{#repeat 5}} | ||
{{setVar 'random' (oneOf (array '1' '2' '3'))}} | ||
{{@random}} | ||
{{/repeat}} | ||
{{setVar 'myArray' (array '1' '2' '3')}} | ||
{{#each @myArray}} | ||
{{setVar 'eachIndex' @index}} | ||
{{@eachIndex}} | ||
{{/repeat}} | ||
``` | ||
|
||
## getVar | ||
|
||
Dynamically get a variable set with [`setVar`](#setvar). | ||
|
||
| Arguments (ordered) | Type | Description | | ||
| ------------------- | ------ | ------------- | | ||
| 0 | string | Variable name | | ||
|
||
**Examples** | ||
|
||
```handlebars | ||
{{setVar 'varname' 'value'}} | ||
{{getVar 'varname'}} | ||
{{getVar (concat 'var' 'name')}} | ||
{{getVar (body 'property')}} | ||
``` | ||
|
||
## setGlobalVar | ||
|
||
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. | ||
|
||
- The variable name and values can be dynamically created using other helpers. | ||
- The variable can store any kind of data (arrays, objects, string, etc.). | ||
- To get the value of a global variable, use the [`{{getGlobalVar 'varName'}}` helper below](#getglobalvar). | ||
|
||
| Arguments (ordered) | Type | Description | | ||
| ------------------- | ------ | -------------- | | ||
| 0 | string | Variable name | | ||
| 1 | any | Variable value | | ||
|
||
**Examples** | ||
|
||
```handlebars | ||
{{setGlobalVar 'varName' 'value'}} | ||
{{setGlobalVar 'varName' (bodyRaw 'id')}} | ||
{{setGlobalVar (queryParam 'param1') (bodyRaw 'id')}} | ||
``` | ||
|
||
## getGlobalVar | ||
|
||
Get a global variable's value set with [`setGlobalVar`](#setglobalvar). Global variables are available on all the routes of an environment and they are reset when the environment is restarted. | ||
|
||
- The variable name and path can be dynamically created using other helpers. | ||
- The `path` supports two syntaxes, [object-path](https://www.npmjs.com/package/object-path) or [JSONPath Plus](https://www.npmjs.com/package/jsonpath-plus). When using object-path, properties containing dots are supported by escaping the dots: `key.key\.with\.dot`. | ||
Please note that a value can be retrieved at the path if the variable contains valid JSON. | ||
- Primitives and data structures can be retrieved by the helper and reused in other helpers (see example below). | ||
- The full variable content (array, object, etc.) can be fetched when the `path` is omitted (`{{getGlobalVar 'varname'}}`). | ||
|
||
| Arguments (ordered) | Type | Description | | ||
| ------------------- | ------ | -------------------------- | | ||
| 0 | string | Variable name | | ||
| 1 | string | Path to the value property | | ||
|
||
**Examples** | ||
|
||
```handlebars | ||
{{getGlobalVar 'varname'}} | ||
{{getGlobalVar (bodyRaw 'property')}} | ||
{{getGlobalVar (urlParam 'id')}} | ||
<!-- Using object-path syntax --> | ||
{{getGlobalVar 'varName' 'path.to.property'}} | ||
{{getGlobalVar 'varName' 'deep.property\.with\.dot'}} | ||
<!-- using JSONPath syntax --> | ||
{{getGlobalVar 'varName' '$.array.[*].property'}} | ||
{{#repeat (getGlobalVar 'varname')}}...{{/repeat}} | ||
{{#each (getGlobalVar 'varName')}}...{{/each}} | ||
<!-- Stringify the variable content --> | ||
{{{stringify (getGlobalVar 'varName')}}} | ||
``` | ||
|
||
## getEnvVar | ||
|
||
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](docs:variables/environment-variables). | ||
|
||
| Arguments (ordered) | Type | Description | | ||
| ------------------- | ------ | ------------------------- | | ||
| 0 | string | Environment variable name | | ||
| 1 | any | Default value | | ||
|
||
**Examples** | ||
|
||
```handlebars | ||
{{setVar 'varname' 'value'}} | ||
{{getEnvVar 'VARIABLE_NAME' 'default value'}} | ||
{{getEnvVar 'VARIABLE_NAME'}} | ||
{{getEnvVar (body 'property')}} | ||
``` |
Oops, something went wrong.