You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
```
0 commit comments