Error with condition in template [solved]

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}}

Count your ends.

I removed one {{ end }} but range causes an unxpected EOF error.

{{ with .Site.Params.address }}
    <p>
        {{ range $i, $e := .Site.Params.address }}
            {{ if $i }}<br>{{ $e }}
        {{ end }}
    </p>
{{ else }}
    <p>3481 Melrose Place<br>Beverly Hills, CA 90210</p>
{{end}}

My bad. There was right amount of ends in your first example. But it would help if you posted the original error.

Hugo throw the following error:

ERROR: 2015/06/05 template: theme/partials/footer.html:52: unexpected EOF
ERROR: 2015/06/05 html/template: "theme/partials/footer.html" is an incomplete template in theme/partials/footer.html

If I remove range everything works fine.

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.

Move the {{ $e }} outside of the if. You want only the <br> to not print for the first item.

Moving the {{ $e }} outside the if let the first line appear but the
tag is placed wrong.

{{ range $i, $e := $.Site.Params.address }}
     {{ $e }}
     {{ if $i }}<br>{{ end }}
{{ end }}

Now the output looks like this where the
is set once at the end:

3481 Melrose Place Beverly Hills, CA 90210 <br>

You probably want something like

{{ 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
1 Like

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.