> For the complete documentation index, see [llms.txt](https://sevenval.gitbook.io/flat/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sevenval.gitbook.io/flat/reference/actions/template.md).

# template Action

The `<template>` action is the primary tool for generating JSON data in FLAT's [*Template Language*](/flat/reference/templating.md).

## Syntax

* `in`: Location of input data

  Either a file (e.g. `in="conf.json"`) or a variable (`in="$body"`). The input sets the template's initial context (`{{.}}`). It may be a JSON or [XML document](/flat/reference/templating/oxn.md).

  `in=""` signals that no input data is used.

  The context can be changed within the template with [`{{with …}}`](/flat/reference/templating/with.md).

  Default: `fit://request/content`
* `out`: Location of output data

  Where the generated JSON should be stored.

  This can be a variable. Variables will hold the *typed result*, such as a JSON object or e.g. a boolean value.

  ```
   <template out="$postBody">
  ```

  Default: `fit://request/content`
* `src`: Location of the template file

  The template definition can be stored in a separate file. The file path is resolved relatively to the flow file.

  ```
   <template src="big.tmpl"/>
  ```

  There is no obligatory file extension. However, we recommend `tmpl` or `tpl`.
* `check`: Check output

  The generated JSON will be parsed and checked. If the template has produced an invalid JSON string, an error will be logged.

  The generated text will still be written to the configured `out` location. In that case, if `out` is a variable, the output is *not typed*.

  Checking can be disabled with `check="false"`.

  Default: `true`

## Examples

### Accessing JSON data received from client's

The [pre-defined variable `$body`](/flat/reference/variables.md#predefined-variables) holds the HTTP body of incoming `POST` requests.

```markup
<flow>
  <template in="$body">
  {
    "inputData": {{.}}
  }
  </template>
</flow>
```

Request with `curl`:

```bash
$ curl -s -H 'content-type: application/json' --data '{"cool":true}' localhost:8080
```

Output:

```javascript
{
  "inputData": {
    "cool": true
  }
}
```

### Accessing JSON data loaded from upstream servers

In this example, we pass the client's `POST` body to [httpbin.org](https://httpbin.org/) to have it reflected back.

```markup
<flow>
  <request>
  {
    "url": "https://httpbin.org/anything",
    "method": "POST",
    "body": {
      "src": "$body"
    }
  }
  </request>

  <template>
  {
    "upstreamResponse": {{.}}
  }
  </template>
</flow>
```

Request with `curl`:

```bash
$ curl -s -H 'content-type: application/json' --data '{"cool":true}' localhost:8080
```

Output:

```javascript
{
  "upstreamResponse": {
    "args": {},
    "data": "{\"cool\":true}",
    "files": {},
    "form": {},
    "headers": {
      "Accept": "*/*",
      "Accept-Encoding": "deflate, gzip",
      "Connection": "close",
      "Content-Length": "13",
      "Content-Type": "application/json",
      "Host": "httpbin.org",
      "User-Agent": "FLAT"
    },
    "json": {
      "cool": true
    },
    "method": "POST",
    "origin": "78.35.14.35",
    "url": "https://httpbin.org/anything"
  }
}
```

### Using a template file

```markup
<flow>
  <request>{ "url": "https://httpbin.org/anything" }</request>
  <template src="modifyResponse.tmpl"/>
</flow>
```

The file path `modifyResponse.tmpl` is resolved relatively to the flow file.

`modifyResponse.tmpl`:

```javascript
{
  "ip": {{ origin }},
  "word": {{ method }}
}
```
