Skip to content

Commit f3eee6c

Browse files
committed
Update template best practices regarding chompin
Signed-off-by: Erik Sundell <[email protected]>
1 parent 90ff5b8 commit f3eee6c

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

content/en/docs/chart_best_practices/templates.md

+59-31
Original file line numberDiff line numberDiff line change
@@ -31,58 +31,81 @@ For that reason, _all defined template names should be namespaced._
3131
Correct:
3232

3333
```yaml
34-
{{- define "nginx.fullname" }}
35-
{{/* ... */}}
36-
{{ end -}}
34+
{{- define "nginx.fullname" -}}
35+
# ...
36+
{{- end }}
3737
```
3838

3939
Incorrect:
4040

4141
```yaml
4242
{{- define "fullname" -}}
43-
{{/* ... */}}
44-
{{ end -}}
43+
# ...
44+
{{- end }}
4545
```
46+
4647
It is highly recommended that new charts are created via `helm create` command
4748
as the template names are automatically defined as per this best practice.
4849

4950
## Formatting Templates
5051

5152
Templates should be indented using _two spaces_ (never tabs).
5253

53-
Template directives should have whitespace after the opening braces and before
54+
Template directives should have whitespace after the opening braces and before
5455
the closing braces:
5556

5657
Correct:
57-
```
58-
{{ .foo }}
59-
{{ print "foo" }}
60-
{{- print "bar" -}}
58+
59+
```yaml
60+
{{- /* Conditionally create service account */}}
61+
{{- if .Values.serviceAccount.create }}
62+
apiVersion: v1
63+
# ...
64+
{{- end }}
6165
```
6266

6367
Incorrect:
68+
69+
```yaml
70+
{{-/* This comment is even a syntax error */}}
71+
{{-if .Values.serviceAccount.create}}
72+
apiVersion: v1
73+
# ...
74+
{{-end}}
6475
```
65-
{{.foo}}
66-
{{print "foo"}}
67-
{{-print "bar"-}}
76+
77+
Templates should chomp unnecessary whitespace. A good practice is to
78+
systematically _chomp left_, but also _chomp right_ for initial content in a
79+
template file or defined template.
80+
81+
```yaml
82+
myList:
83+
{{- range .Values.elements }}
84+
- {{ . }
85+
{{- end }}
6886
```
6987

70-
Templates should chomp whitespace where possible:
88+
```yaml
89+
{{- define "nginx.selectorLabels" -}}
90+
app.kubernetes.io/name: {{ include "nginx.name" . }}
91+
app.kubernetes.io/instance: {{ .Release.Name }}
92+
{{- end }}
93+
```
7194

7295
```yaml
73-
foo:
74-
{{- range .Values.items }}
75-
{{ . }}
76-
{{ end -}}
96+
{{- if .Values.serviceAccount.create -}}
97+
apiVersion: v1
98+
# ...
99+
{{- end }}
77100
```
78101

79102
Blocks (such as control structures) may be indented to indicate flow of the
80103
template code.
81104

82-
```
83-
{{ if $foo -}}
84-
{{- with .Bar }}Hello{{ end -}}
85-
{{- end -}}
105+
```yaml
106+
{{- range .Values.elements }}
107+
{{- print "my element is" . }}
108+
{{- end }}
86109
```
87110

88111
However, since YAML is a whitespace-oriented language, it is often not possible
@@ -147,17 +170,23 @@ metadata:
147170
Both YAML and Helm Templates have comment markers.
148171

149172
YAML comments:
173+
150174
```yaml
151175
# This is a comment
152176
type: sprocket
177+
foo: bar # This is a comment, bar isn't
153178
```
154179
155-
Template Comments:
180+
Helm template comments:
181+
156182
```yaml
157-
{{- /*
158-
This is a comment.
159-
*/ -}}
183+
{{- /* This is a comment (with chomping to the right). */ -}}
160184
type: frobnitz
185+
186+
{{- /*
187+
This is a multiline comment (with no chomping to the right).
188+
*/}}
189+
foo: bar
161190
```
162191
163192
Template comments should be used when documenting features of a template, such
@@ -166,11 +195,10 @@ as explaining a defined template:
166195
```yaml
167196
{{- /*
168197
mychart.shortname provides a 6 char truncated version of the release name.
169-
*/ -}}
170-
{{ define "mychart.shortname" -}}
171-
{{ .Release.Name | trunc 6 }}
172-
{{- end -}}
173-
198+
*/}}
199+
{{- define "mychart.shortname" -}}
200+
{{ .Release.Name | trunc 6 | trimSuffix "-" }}
201+
{{- end }}
174202
```
175203

176204
Inside of templates, YAML comments may be used when it is useful for Helm users

0 commit comments

Comments
 (0)