Whitespace Control
Dashes inside the braces ({{- and -}}) trim surrounding whitespace so rendered YAML stays valid.
{{- action }} or {{ action -}}Template actions leave behind blank lines and stray spaces that can break YAML. Go templates give you whitespace trimming with a dash.
The dash
{{-trims all whitespace (including the newline) before the action.-}}trims whitespace after the action.
So {{- if .x }} removes the preceding newline that the template line would otherwise leave, keeping your output tidy.
Newlines and indent
Be careful: -}} eats the newline after it too, which can join lines unexpectedly. Pair trimming with nindent when inserting blocks so indentation is always correct.
Example
# Without trimming, the if leaves a blank line:
metadata:
labels:
{{- if .Values.extraLabels }}
{{- toYaml .Values.extraLabels | nindent 4 }}
{{- end }}
# {{- trims the newline before, keeping the block cleanWhen to use it
- A chart author adds {{- to every action block to strip the preceding newline, preventing extra blank lines in the rendered YAML that confuse strict parsers.
- A developer uses -}} at the end of a define block to consume the trailing newline, so the helper does not inject unwanted blank lines when included.
- A platform team runs helm template --debug and inspects rendered YAML for stray indentation or blank lines caused by missing whitespace dashes before adding them to templates.
More examples
Trim with leading dash
The hyphen in {{- trims the whitespace (including newlines) that precedes the action, removing unwanted blank lines.
# Without dash: blank line before 'replicas'
spec:
{{ .Values.replicaCount }}
# With dash: no blank line
spec:
replicas: {{- .Values.replicaCount }}
# Leading dash eats the newline before the expressionTrailing dash in define block
Placing dashes on both sides of define and end ensures the helper emits only its return value without surrounding whitespace.
{{- define "myapp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end -}}
# Both dashes strip newlines so include inserts only the valueWhitespace around if block
Uses leading dashes on if, toYaml, and end to remove the blank lines each action would otherwise leave in the rendered YAML.
spec:
{{- if .Values.tolerations }}
tolerations:
{{- toYaml .Values.tolerations | nindent 4 }}
{{- end }}
containers:
Discussion