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:

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

Template:

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

Output:

{
"Name": "alice"
}

Example: with and json-doc()

{{with}} is especially useful in conjunction with json-doc() to consolidate data from multiple sources using more compact expressions.

Input:

{
"location": "home"
}

Input fit://request/request/body:

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

Template:

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

Output:

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