Implementing Template Whitespace Control
1. Understanding Whitespace Behavior
| Control Type | Description | Example |
|---|---|---|
| Trim Whitespace | Removes leading and trailing whitespace from template elements. | {{- name -}} |
| Suppress Newlines | Prevents newlines from appearing in rendered output. | {{- .Values.port -}} |
| Preserve Indentation | Keeps indentation as written in the template. | {{ name }} {{ .Values.name }} |
Note: The
- operator trims whitespace from the left or right of a template element.
Warning: Misusing whitespace control can lead to unexpected formatting in rendered templates.
2. Using Left Trim ({{-)
The left trim operator {{- in Helm templates removes whitespace (including newlines) from the left side of the template expression. It is particularly useful for controlling whitespace in rendered output, especially when generating multi-line content.
| Operator | Description | Example | Result |
|---|---|---|---|
{{- |
Trims whitespace to the left of the expression | {{- " Hello" }} |
Hello |
Example: Left Trim in Multi-line Template
<div>
<h1>{{- .Values.title }}</h1>
<p>{{- .Values.description }}</p>
</div>
Note: The left trim operator does not affect content inside quotes or within other template constructs like
{{ if }} or {{ range }}.
3. Using Right Trim (-}})
In Helm Charts, whitespace control is crucial for managing the output formatting of templates. The right trim operator (-}}) allows you to remove whitespace from the end of a template block, ensuring clean output without unnecessary trailing spaces or newlines.
| Template Syntax | Description | Effect |
|---|---|---|
{{- .Value -}} |
Trims whitespace from both ends of the value | Removes leading and trailing whitespace from rendered content |
{{- .Value }} |
Trims only trailing whitespace | Removes trailing whitespace but keeps leading whitespace |
{{ .Value -}} |
Trims only leading whitespace | Removes leading whitespace but keeps trailing whitespace |
Example: Using Right Trim in a List
{{- range .Values.items }}
- {{ .name }}: {{ .value }}
{{- end -}}
Example: Trimming Whitespace in a Multi-line Block
{{- $items := .Values.items }}
{{- range $items }}
{{- .name }}: {{ .value }}
{{- end -}}
Note: The right trim operator (
-}}) is particularly useful in loops or conditional blocks where extra newlines or spaces can affect the final rendered output. It ensures that templates produce clean, predictable results.
4. Combining Trim Markers ({{- -}})
| Marker | Description | Effect | Example |
|---|---|---|---|
| `{{-` | Trims whitespace to the left of the tag | Removes all whitespace (spaces, tabs, newlines) before the tag | hello {{- world }}! |
| `-}}` | Trims whitespace to the right of the tag | Removes all whitespace (spaces, tabs, newlines) after the tag | hello {{ world -}}! |
| `{{- -}}` | Trims whitespace on both sides of the tag | Removes all whitespace before and after the tag | hello {{- world -}}! |
Example: Using {{- -}} to Control Whitespace
template:
- name: example
content: |
<div>
{{- range .Values.items -}}
<p>{{ . }}</p>
{{- end -}}
</div>
Note: The trim markers are particularly useful in multi-line templates to prevent extra newlines or spaces from appearing in the rendered output.
5. Controlling Indentation
| Directive | Description | Usage |
|---|---|---|
| indent | Controls indentation of template blocks | {{- indent 2 " " -}} |
| nindent | Same as indent but with newlines | {{- nindent 4 " " -}} |
Example: Using Indent
{{- nindent 2 " " -}}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
targetPort: 8080
Note: The
- in {{- ... -}} removes whitespace before and after the directive.6. Managing YAML Formatting
| Directive | Description | Usage | Notes |
|---|---|---|---|
| {{- | Removes whitespace from the left side of the directive | {{- .Values.name }} |
Useful for aligning YAML structure |
| -}} | Removes whitespace from the right side of the directive | {{ .Values.name -}} |
Ensures clean formatting in YAML output |
| {{- -}} | Removes whitespace from both sides of the directive | {{- .Values.name -}} |
Best for strict YAML formatting control |
Example: YAML Formatting Control
apiVersion: v1
kind: ConfigMap
metadata:
name: {{- .Values.name -}}
data:
config.yaml: |
{{- .Values.configContent | indent 4 -}}
Note: Whitespace control is essential for generating clean, valid YAML in Helm templates.
7. Using Template Comments ({{/* */}})
Template comments in Helm charts are used to add explanatory notes or disable parts of a template without affecting the rendered output. They are not included in the final rendered manifest.
| Feature | Description | Usage |
|---|---|---|
| Template Comment Syntax | Comments in Helm templates use the {{/* */}} syntax. |
{{/* This is a comment */}} |
| Comment Placement | Comments can be placed anywhere in the template and do not affect rendering. | |
Example: Commenting Out Configuration
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-config
data:
# {{/* This section is commented out */}}
# debug: "true"
logLevel: "info"
8. Avoiding Extra Blank Lines
| Control | Description | Example |
|---|---|---|
| - | Whitespace control in templates | {{- .Values.name -}} |
Example: Controlling Whitespace in Helm Templates
{{- define "myapp.name" -}}
{{- .Values.name -}}
{{- end -}}
Note: The
- operator removes whitespace before and after the expression.
Warning: Misusing whitespace control can lead to unexpected formatting in rendered templates.
9. Formatting Multi-line Strings (| and >)
| Operator | Description | Behavior | Example |
|---|---|---|---|
| | | Strip whitespace from the left side of a line | Removes leading whitespace from each line in a multi-line string | {{- .Values.myValue | indent 4 -}} |
| > | Strip whitespace from the right side of a line | Removes trailing whitespace from each line in a multi-line string | {{- .Values.myValue | nindent 4 -}} |
Example: Using | to format multi-line strings
template:
content: |
{{- .Values.myValue | indent 2 -}}
Example: Using > to format multi-line strings
template:
content: >
{{- .Values.myValue | nindent 2 -}}
Note: The
| operator preserves newlines, while the > operator collapses multiple newlines into a single space.
10. Debugging Whitespace Issues
Example: Identifying Extra Spaces in Rendered Template
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-config
data:
config.yaml: |
server:
port: {{ .Values.server.port }}
host: {{ .Values.server.host }}
| Issue | Description | Solution |
|---|---|---|
| Extra Spaces in Values | Whitespace around values in templates can cause rendering issues. | Use {{- .Values.server.port -}} to trim whitespace. |
| Newlines in Multi-line Values | Multi-line values may introduce unwanted newlines or spaces. | Use {{- printf "%s" .Values.server.config -}} to control formatting. |
Note: Use
- (dash) in template tags to strip whitespace.
Warning: Overuse of
- can make templates harder to read and debug.