Templates
{{ mustache }} templates can be used to define custom commands and search templates.
The template test
command offers a way to test templates. We use this functionality below to illustrate template usage.
Template Parameter
A template parameter has a name and is surrounded by curly braces {{name}}
. The parameter with the curly braces gets replaced by the value of the parameter when the template is rendered.
# template definition: Hello {{name}} - I feel {{mood}}.
# value definition: name=John, mood=happy
> template test name=John, mood=happy Hello {{name}} - I feel {{mood}}.
Hello John - I feel happy.
If/Then Parameter
It is possible to guard a template section. The if/then template is put into a start and end section {{#prop}} .. {{/prop}}
. The template is rendered, if the parameter prop
is not empty and not false.
# name is defined - the hello section is rendered.
> template test name=John {{#name}}Hello {{name}}. {{/name}}Nice to meet you!
Hello John. Nice to meet you!
# name is false - the hello section is omitted.
> template test name=false {{#name}}Hello {{name}}. {{/name}}Nice to meet you!
Nice to meet you!
# name is not defined - the hello section is omitted.
> template test {{#name}}Hello {{name}}. {{/name}}Nice to meet you!
Nice to meet you!
If/then parameters can also be negated using the {{^prop}} .. {{/prop}}
syntax. The inner section is only rendered, if prop
is false or undefined.
# name is defined - the hello section is rendered.
> template test name=John {{^name}}Hello stranger. {{/name}}Nice to meet you!
Nice to meet you!
# name is false - the hello section is omitted.
> template test name=false {{^name}}Hello stranger. {{/name}}Nice to meet you!
Hello stranger. Nice to meet you!
# name is not defined - the hello section is omitted.
> template test {{^name}}Hello stranger. {{/name}}Nice to meet you!
Hello stranger. Nice to meet you!
List Parameter
It is possible to render a list of values. The list template is put into a start and end section {{#list}} .. {{/list}}
. Every element of the list is passed to the section content.
> template test ls=[{name:Lisa},{name:Bart},{name:Maggie}] {{#ls}}The name is {{name}}. {{/ls}}
The name is Lisa. The name is Bart. The name is Maggie.
Special Functions
with_index
A list parameter can be called with the with_index
function. Every element of the list will be enriched with the following properties:
first
: boolean, true if the element is the first element of the listlast
: boolean, true if the element is the last element of the listindex
: integer, the index of the element in the list
> template test ls=[{name:Lisa},{name:Bart},{name:Maggie}] {{#ls.with_index}}
The name of the {{#first}}first {{/first}}{{#last}}last {{/last}}person is {{name}}({{index}}). {{/ls.with_index}}
The name of the first person is Lisa(0).
The name of the person is Bart(1).
The name of the last person is Maggie(2).
from_now
A string parameter is interpreted as duration and can be translated into a date using the current time.
> template test duration=3d After the duration of {{duration}} it will be {{duration.from_now}}.
After the duration of 3d it will be 2022-04-22T10:53:01Z.