Core Functionality
Before to check examples, we need to know about comments and whitespace.
{{/* a comment */}}
{{- /* a comment with white space trimmed from preceding and following text */ -}}
A comment; discarded. May contain newlines.
Comments do not nest and must start and end at the
delimiters, as shown here.
Minus sign (-) is a special character that represents white space trimming.
Merhaba
{{- " " -}}
dünya!
Output:
Merhaba dünya!
Variables
Inside of template, we can use variables. Variables are defined by $variableName := value syntax.
{{ $x := "Merhaba" -}}
{{ $x }} dünya!
Output:
Merhaba dünya!
For loop
If you have a list of items, you can iterate over them using the range
function.
list:
- "item-1"
- "item-2"
Range of list
{{ range $index, $element := .list -}}
{{ $index }}: {{ $element }}
{{ end }}
Output:
Range of list
0: item-1
1: item-2
If we want to for loop with a map, in that time $index is a key of map and $element is a value of map.
We can also use range
to count up to a number. But we need to use until
function and it's a part of sprig
functions. Mugo has a sprig
function set by default so we can use it.
Count up to 5
{{ range $index, $element := until 5 -}}
{{ $index }}: {{ $element }}
{{ end }}
Output:
Count up to 5
0: 0
1: 1
2: 2
3: 3
4: 4
If statement
Binary check functions eq
, ne
, lt
, le
, gt
, ge
but arguments should comparable types and result is boolean.
result: 10
{{if eq .result 10 -}}
Result is 10
{{- else if eq .result 0 -}}
Result is 0
{{- else -}}
unknown
{{- end}}
Output:
Result is 10
Index
We can use index
function to get an item from a list.
list:
- "item-1"
- "item-2"
First item of list: {{ index .list 0 }}
Output:
First item of list: item-1
Define
We can use define to define a template and use it later with template
function and giving variables.
{{- define "hello" -}}
Hello {{ .name }}
{{- end -}}
{{ template "hello" . }}
Output:
Merhaba dünya!
With
Use with to limit the scope of a variable inside of a template.
If name
value is empty, it will not print anything.
{{ with .name -}}
Merhaba {{ . }}
{{- end }}
If we want to reach outer scope, we can use $.
syntax.
{{ with .name -}}
Item {{ . }} value is {{ $.value }}
{{- end }}
Examples
Some useful examples with templates.
Sum of variables
addf
function is a part of sprig
functions. It uses decimal library to calculate floating point numbers.
values:
- 6.1
- 5.81
- 7.9
{{ $sum := 0 -}}
{{ range $index, $element := .values -}}
{{ $sum = addf $sum $element -}}
{{ end -}}
Total value {{ $sum }}
Output:
Total value 19.81