Commit 2dd6ed3
authored
Remove existence check from templates before range (#8181)
Closes #8095
Per testing and documentation the if guard to check if a slice is not
empty or not nil is not necessary.
Documentation:
> {{range pipeline}} T1 {{end}}
> The value of the pipeline must be an array, slice, map, iter.Seq,
> iter.Seq2, integer or channel.
> If the value of the pipeline has length zero, nothing is output;
> otherwise, dot is set to the successive elements of the array,
> slice, or map and T1 is executed. If the value is a map and the
> keys are of basic type with a defined order, the elements will be
> visited in sorted key order.
https://pkg.go.dev/text/template#hdr-Actions
Code testing:
```
<p>begin</p>
{{ range $value := .Elements }}
<p>{{ $value }}</p>
{{ end }}
<p>end</p>
```
struct that gets populated with data:
```
type Example struct {
Elements []string
}
```
executed with the following data:
```
// slice is not empty
e := Example{
Elements: []string{"one", "two", "three"}
}
// slice is empty, but initialised
eEmpty := Example {
Elements: []string{}
}
// slice is nil
eNil := Example {
Elements: nil
}
```
The output for these three in order:
```
<p>begin</p>
<p>Element 1</p>
<p>Element 2</p>
<p>Element 3</p>
<p>end</p>
<p>begin</p>
<p>end</p>
<p>begin</p>
<p>end</p>
```1 parent 7801be3 commit 2dd6ed3
File tree
4 files changed
+14
-32
lines changed- internal/configs/version1
4 files changed
+14
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
216 | | - | |
217 | 216 | | |
218 | 217 | | |
219 | | - | |
220 | 218 | | |
221 | 219 | | |
222 | 220 | | |
| |||
264 | 262 | | |
265 | 263 | | |
266 | 264 | | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
| 265 | + | |
| 266 | + | |
271 | 267 | | |
272 | 268 | | |
273 | 269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 25 | + | |
| 26 | + | |
29 | 27 | | |
30 | 28 | | |
31 | 29 | | |
| |||
42 | 40 | | |
43 | 41 | | |
44 | 42 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 43 | + | |
| 44 | + | |
49 | 45 | | |
50 | 46 | | |
51 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
111 | 110 | | |
112 | | - | |
113 | | - | |
| 111 | + | |
114 | 112 | | |
115 | 113 | | |
116 | 114 | | |
| |||
137 | 135 | | |
138 | 136 | | |
139 | 137 | | |
140 | | - | |
141 | 138 | | |
142 | | - | |
143 | | - | |
| 139 | + | |
144 | 140 | | |
145 | 141 | | |
146 | 142 | | |
| |||
183 | 179 | | |
184 | 180 | | |
185 | 181 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
| 182 | + | |
| 183 | + | |
190 | 184 | | |
191 | 185 | | |
192 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 18 | + | |
| 19 | + | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
| |||
36 | 34 | | |
37 | 35 | | |
38 | 36 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
| 37 | + | |
| 38 | + | |
43 | 39 | | |
44 | 40 | | |
45 | 41 | | |
| |||
0 commit comments