@@ -31,58 +31,81 @@ For that reason, _all defined template names should be namespaced._
31
31
Correct:
32
32
33
33
``` yaml
34
- {{- define "nginx.fullname" }}
35
- {{/* ... */}}
36
- {{ end - }}
34
+ {{- define "nginx.fullname" - }}
35
+ # ...
36
+ {{- end }}
37
37
```
38
38
39
39
Incorrect:
40
40
41
41
``` yaml
42
42
{{- define "fullname" -}}
43
- {{/* ... */}}
44
- {{ end - }}
43
+ # ...
44
+ {{- end }}
45
45
```
46
+
46
47
It is highly recommended that new charts are created via ` helm create ` command
47
48
as the template names are automatically defined as per this best practice.
48
49
49
50
## Formatting Templates
50
51
51
52
Templates should be indented using _ two spaces_ (never tabs).
52
53
53
- Template directives should have whitespace after the opening braces and before
54
+ Template directives should have whitespace after the opening braces and before
54
55
the closing braces:
55
56
56
57
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 }}
61
65
```
62
66
63
67
Incorrect:
68
+
69
+ ``` yaml
70
+ {{-/* This comment is even a syntax error */}}
71
+ {{-if .Values.serviceAccount.create}}
72
+ apiVersion : v1
73
+ # ...
74
+ {{-end}}
64
75
```
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 }}
68
86
```
69
87
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
+ ```
71
94
72
95
``` yaml
73
- foo :
74
- {{- range .Values.items }}
75
- {{ . }}
76
- {{ end - }}
96
+ {{- if .Values.serviceAccount.create -}}
97
+ apiVersion : v1
98
+ # ...
99
+ {{- end }}
77
100
```
78
101
79
102
Blocks (such as control structures) may be indented to indicate flow of the
80
103
template code.
81
104
82
- ` ` `
83
- {{ if $foo - }}
84
- {{- with .Bar }}Hello{{ end - }}
85
- {{- end - }}
105
+ ``` yaml
106
+ {{- range .Values.elements }}
107
+ {{- print "my element is" . }}
108
+ {{- end }}
86
109
```
87
110
88
111
However, since YAML is a whitespace-oriented language, it is often not possible
@@ -147,17 +170,23 @@ metadata:
147
170
Both YAML and Helm Templates have comment markers.
148
171
149
172
YAML comments:
173
+
150
174
``` yaml
151
175
# This is a comment
152
176
type : sprocket
177
+ foo : bar # This is a comment, bar isn't
153
178
` ` `
154
179
155
- Template Comments:
180
+ Helm template comments:
181
+
156
182
` ` ` yaml
157
- {{- /*
158
- This is a comment.
159
- */ -}}
183
+ {{- /* This is a comment (with chomping to the right). */ -}}
160
184
type : frobnitz
185
+
186
+ {{- /*
187
+ This is a multiline comment (with no chomping to the right).
188
+ */}}
189
+ foo : bar
161
190
` ` `
162
191
163
192
Template comments should be used when documenting features of a template, such
@@ -166,11 +195,10 @@ as explaining a defined template:
166
195
` ` ` yaml
167
196
{{- /*
168
197
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 }}
174
202
```
175
203
176
204
Inside of templates, YAML comments may be used when it is useful for Helm users
0 commit comments