Skip to content

Commit 8d74a71

Browse files
committed
Template best practices: declared template indentation practice
1 parent 0b81002 commit 8d74a71

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

content/en/docs/chart_best_practices/templates.md

+83-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ as the template names are automatically defined as per this best practice.
4949

5050
## Formatting Templates
5151

52-
Templates should be indented using _two spaces_ (never tabs).
53-
5452
Template directives should have whitespace after the opening braces and before
5553
the closing braces:
5654

@@ -91,8 +89,91 @@ app.kubernetes.io/instance: {{ .Release.Name }}
9189
apiVersion: v1
9290
```
9391
92+
Template logic should align indentation with associated content.
93+
94+
Correct:
95+
96+
```yaml
97+
metadata:
98+
annotations:
99+
{{- if .Values.foo }}
100+
foo: true
101+
{{- end }}
102+
bar: true
103+
{{- with .Values.extraAnnotations }}
104+
{{- . | toYaml | nindent 4 }}
105+
{{- end }}
106+
```
107+
108+
Incorrect:
109+
110+
```yaml
111+
metadata:
112+
annotations:
113+
{{- if .Values.foo }}
114+
foo: true
115+
{{- end }}
116+
bar: true
117+
{{- with .Values.extraAnnotations }}
118+
{{- . | toYaml | indent 4 }}
119+
{{- end }}
120+
```
121+
122+
To align templates logic with associated content, the `nindent` function
123+
together with left whitespace chomping is often required. `nindent` works
124+
exactly like `indent` but prefixes a new line that can compensate for our left
125+
whitespace chomping.
126+
127+
Correct:
128+
129+
```yaml
130+
metadata:
131+
annotations:
132+
foo: true
133+
{{- with .Values.extraAnnotations }}
134+
{{- . | toYaml | nindent 4 }}
135+
{{- end }}
136+
```
137+
138+
Incorrect:
139+
140+
```yaml
141+
metadata:
142+
annotations:
143+
foo: true
144+
{{- with .Values.extraAnnotations }}
145+
{{- . | toYaml | indent 4 }}
146+
{{- end }}
147+
```
148+
94149
## Whitespace in Generated Templates
95150

151+
Generated content should be indented with increments of _two spaces_.
152+
153+
Correct:
154+
155+
```yaml
156+
apiVersion: batch/v1
157+
kind: Job
158+
metadata:
159+
name: example
160+
labels:
161+
first: first
162+
second: second
163+
```
164+
165+
Incorrect:
166+
167+
```yaml
168+
apiVersion: batch/v1
169+
kind: Job
170+
metadata:
171+
name: example
172+
labels:
173+
first: first
174+
second: second
175+
```
176+
96177
It is preferable to keep the amount of whitespace in generated templates to a
97178
minimum. In particular, numerous blank lines should not appear adjacent to each
98179
other. But occasional empty lines (particularly between logical sections) is

0 commit comments

Comments
 (0)