I can both use
{{- range .Values.xxx }}
and
{{ range .Values.xxx }}
in my helm, and the result is same. are they totally the same thing?The Hyphen is not necessary?
The hyphen removes any whitespace adjacent to the template construct. Often this isn't strictly necessary but it makes the rendered output more readable. Since YAML is whitespace-sensitive sometimes it is required.
Basic example:
environment:
{{ range .Values.xxx }}
- { name: {{ . }}, value: "test" }
{{ end }}
Renders to:
environment:
- { name: a, value: "test" }
- { name: b, value: "test" }
On the range
line, after the }}
and before the -
, there is a newline and two spaces; these are required for it to be valid YAML. Before both of the {{
there is an additional newline and this isn't required, so you can use {{-
to suppress it.
environment:
{{- range .Values.xxx }}
- { name: {{ . }}, value: "test" }
{{- end }}
environment:
- { name: a, value: "test" }
- { name: b, value: "test" }
This is also useful with functions like nindent
that include their own newlines.
items: {{- .Values.xxx | toYaml | nindent 2 }}
items:
- a
- b
toYaml
converts the list [a, b]
to the YAML string - a\n- b
; nindent 2
adds an initial newline, and two spaces at the start of each line \n - a\n - b
; and then the -
removes the extra space before the newline that's being emitted.
You can put the -
inside the closing }}
too, with the same effect, but that tends to eat the next line's indentation, so it tends to be a little less useful.
(Remember that helm template
will render a chart to stdout without sending it off to the Kubernetes API server, so you can see what these things expand to.)