@@ -31,58 +31,77 @@ 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
+ {{- .foo }}
61
+ {{- print "foo" }}
62
+ {{- print "bar" }}
61
63
```
62
64
63
65
Incorrect:
64
- ```
66
+
67
+ ``` yaml
65
68
{{.foo}}
66
69
{{print "foo"}}
67
70
{{-print "bar"-}}
68
71
```
69
72
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.
71
76
72
77
``` 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 }}
77
96
```
78
97
79
98
Blocks (such as control structures) may be indented to indicate flow of the
80
99
template code.
81
100
82
- ` ` `
83
- {{ if $foo - }}
84
- {{- with .Bar }}Hello{{ end - }}
85
- {{- end - }}
101
+ ``` yaml
102
+ {{- if $foo }}
103
+ {{- with .Bar }}Hello{{- end }}
104
+ {{- end }}
86
105
```
87
106
88
107
However, since YAML is a whitespace-oriented language, it is often not possible
@@ -147,17 +166,23 @@ metadata:
147
166
Both YAML and Helm Templates have comment markers.
148
167
149
168
YAML comments:
169
+
150
170
``` yaml
151
171
# This is a comment
152
172
type : sprocket
173
+ foo : bar # This is a comment, bar isn't
153
174
` ` `
154
175
155
- Template Comments:
176
+ Helm template comments:
177
+
156
178
` ` ` yaml
157
- {{- /*
158
- This is a comment.
159
- */ -}}
179
+ {{- /* This is a comment (with chomping to the right). */ -}}
160
180
type : frobnitz
181
+
182
+ {{- /*
183
+ This is a multiline comment (with no chomping to the right).
184
+ */}}
185
+ foo : bar
161
186
` ` `
162
187
163
188
Template comments should be used when documenting features of a template, such
@@ -166,11 +191,10 @@ as explaining a defined template:
166
191
` ` ` yaml
167
192
{{- /*
168
193
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 }}
174
198
```
175
199
176
200
Inside of templates, YAML comments may be used when it is useful for Helm users
0 commit comments