Sub topic 7
Controlling whitespace with {{- and -}}
This chapter assumes you can read basic Helm templates, know how .Values works, and have helm template available. Whitespace matters here because YAML uses indentation to define structure. A stray blank line is harmless, but a wrongly indented line breaks the manifest.
Why templates leave blank lines
Helm renders a template as plain text. Each {{ ... }} action is replaced by its output, and everything around it stays, including the newline after the closing braces. An action that prints nothing, such as if or end, still leaves its line break behind.
spec:
replicas: {{ .Values.replicaCount }}
{{- if .Values.debug }}
debug: true
{{- end }}
strategy: RollingUpdateWithout the dashes, the if and end lines would each leave an empty line in the output. That is usually valid YAML but untidy, and it can break block scalars and indentation-sensitive output.
The two trim markers
{{-removes all whitespace, including newlines, immediately before the action.-}}removes all whitespace, including newlines, immediately after the action.
The dash must touch the braces and be separated from the action by a space. {{- if trims. {{-if is not a trim marker, and {{ -1 }} is the number minus one.
labels:
app: web
{{- if .Values.team }}
team: {{ .Values.team }}
{{- end }}With team: platform, the output is:
labels:
app: web
team: platformThe {{- before if eats the newline and indentation that precede it. Then the text after }} supplies a fresh newline and two spaces before team:. With team unset, the whole block disappears and app: web is the last line.
Trimming too much
Trimming on the right can join lines you wanted separate.
name: {{ .Values.name -}}
version: 1This renders as name: webversion: 1, because -}} removed the newline. Use -}} only when you mean to join. The common pattern is a left trim ({{-) on control actions, with the right side left alone.
Inspecting the result
Render the chart and look at the output directly.
helm template my-release ./webapp --set team=platform
helm template my-release ./webapp | cat -A | head -n 30cat -A shows line endings as $ and tabs as ^I, which makes stray blank lines and trailing spaces visible.
Summary table
| Marker | Removes | Typical use |
|---|---|---|
| `{{-` | Whitespace before the action | Control lines such as `if`, `range`, `end` |
| `-}}` | Whitespace after the action | Joining output on one line, `define` blocks |
| `{{ }}` | Nothing | Inline values like `{{ .Values.port }}` |
Exercise
This template is meant to render port: 80 followed by protocol: TCP on the next line, but it renders port: 80protocol: TCP. What is wrong, and how do you fix it?
port: {{ .Values.port -}}
protocol: TCP
Show answer
The -}} after .Values.port removes the newline that separates the two lines. Change it to }} so the newline is kept: port: {{ .Values.port }}.