My template checks, if the address array is set in the configs so that the templates can interate over the array to display single lines. Is the variable not set, a default value should be displayed.
{{ with .Site.Params.address }}
<p>{{ range $i, $e := .Site.Params.address }}{{ if $i }} <br> {{ end }}{{ $e }}{{ end }}</p>
{{ else }}
<p>3481 Melrose Place<br>Beverly Hills, CA 90210</p>
{{end}}
I’ve solved the problem partly by adding the missing {{end}} and by adding a $ in in front of Site.Params.address.
{{ with .Site.Params.address }}
<p>
{{ range $i, $e := $.Site.Params.address }}
{{ if $i }}{{ $e }}<br>{{ end }}
{{ end }}
</p>
{{ else }}
<p>3481 Melrose Place<br>Beverly Hills, CA 90210</p>
{{end}}
But the first index of the config array isn’t shown:
address:
- 3481 Melrose Place
- Just for testing
- Beverly Hills, CA 90210
3481 Melrose Place isn’t rendered in the template.
{{ range $i, $e := $.Site.Params.address }}
{{ if $i }}<br>{{ end }}
{{ $e }}
{{ end }}
The {{ if $i }} is shorthand for if $i != 0. The first item of the array has index 0, so the <br> won’t print for the first item, but it will for all the rest of the items.
The above should output something like
3481 Melrose Place
<br>Just for testing
<br>Beverly Hills, CA 90210
Finally, this is what I wanted. But it’s good to know that if $i != 0 is a shorthand. Before I thought, I would just check, whether the current index exists or not.