Skip to content

Commit b164ecb

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

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

content/en/docs/chart_best_practices/templates.md

+53-29
Original file line numberDiff line numberDiff line change
@@ -31,58 +31,77 @@ 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+
{{- .foo }}
61+
{{- print "foo" }}
62+
{{- print "bar" }}
6163
```
6264

6365
Incorrect:
64-
```
66+
67+
```yaml
6568
{{.foo}}
6669
{{print "foo"}}
6770
{{-print "bar"-}}
6871
```
6972

70-
Templates should chomp whitespace where possible:
73+
Templates should chomp unnecessary whitespace. A good practice is to
74+
systematically _chomp left_, but also _chomp right_ for initial content in a
75+
template file or defined template.
7176

7277
```yaml
73-
foo:
74-
{{- range .Values.items }}
75-
{{ . }}
76-
{{ end -}}
78+
myList:
79+
{{- range .Values.elements }}
80+
- {{ . }
81+
{{- end }}
82+
```
83+
84+
```yaml
85+
{{- define "nginx.selectorLabels" -}}
86+
app.kubernetes.io/name: {{ include "nginx.name" . }}
87+
app.kubernetes.io/instance: {{ .Release.Name }}
88+
{{- end }}
89+
```
90+
91+
```yaml
92+
{{- if .Values.serviceAccount.create -}}
93+
apiVersion: v1
94+
# ...
95+
{{- end }}
7796
```
7897

7998
Blocks (such as control structures) may be indented to indicate flow of the
8099
template code.
81100

82-
```
83-
{{ if $foo -}}
84-
{{- with .Bar }}Hello{{ end -}}
85-
{{- end -}}
101+
```yaml
102+
{{- if $foo }}
103+
{{- with .Bar }}Hello{{- end }}
104+
{{- end }}
86105
```
87106

88107
However, since YAML is a whitespace-oriented language, it is often not possible
@@ -147,17 +166,23 @@ metadata:
147166
Both YAML and Helm Templates have comment markers.
148167

149168
YAML comments:
169+
150170
```yaml
151171
# This is a comment
152172
type: sprocket
173+
foo: bar # This is a comment, bar isn't
153174
```
154175
155-
Template Comments:
176+
Helm template comments:
177+
156178
```yaml
157-
{{- /*
158-
This is a comment.
159-
*/ -}}
179+
{{- /* This is a comment (with chomping to the right). */ -}}
160180
type: frobnitz
181+
182+
{{- /*
183+
This is a multiline comment (with no chomping to the right).
184+
*/}}
185+
foo: bar
161186
```
162187
163188
Template comments should be used when documenting features of a template, such
@@ -166,11 +191,10 @@ as explaining a defined template:
166191
```yaml
167192
{{- /*
168193
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-
194+
*/}}
195+
{{- define "mychart.shortname" -}}
196+
{{ .Release.Name | trunc 6 | trimSuffix "-" }}
197+
{{- end }}
174198
```
175199

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

0 commit comments

Comments
 (0)