# with

The template command `{{with _expression_}}` sets the context to the result of `expression` and executes the enclosed expressions or executes the corresponding `{{else}}` block if the expression evaluates to `null`.

Input:

```javascript
{
  "user": {
    "name": "alice",
  }
}
```

Template:

```markup
<template>
{
  {{with ./user}}
    "Name": {{ name }}
  {{else}}
    "Name": "unknown"
  {{end}}
}
</template>
```

Output:

```javascript
{
  "Name": "alice"
}
```

## Example: `with` and `json-doc()`

`{{with}}` is especially useful in conjunction with [`json-doc()`](https://sevenval.gitbook.io/flat/develop/reference/functions/json-doc) to consolidate data from multiple sources using more compact expressions.

Input:

```javascript
{
  "location": "home"
}
```

Input `fit://request/request/body`:

```javascript
{
  "user": "fred",
  "pass": "wilma"
}
```

Template:

```markup
<template>
{
  "realm": {{ location }},
  {{with json-doc("fit://request/request/body") }}
    "user": {{ user }},
    "password": {{ pass }}
  {{end}}
}
</template>
```

Output:

```javascript
{
  "realm": "home",
  "user": "wilma",
  "password": "fred"
}
```
