Hugo 0.41 - check for last item in Site.Data.any sorted range

I have the following snippet:

{{ range $index, $element := sort “weight” }}

I want to check when enumerating if the actual item is the last one.
I already tried several attempts, but all failed.

Do anyone knows how to check if actual item in Site.Data enumeration (when sorted) is the last item?!

Thanks in advance!

So maybe you could do something like this (I’m leaving your sorting out for simplicity):

{{ $count := | len }}
{{ range $index, $element := }}

{{ $.Scratch.Add "i" 1 }}
{{ $i := $.Scratch.Get "i" }}

{{ if eq $count $i }}LAST {{ end }}

{{ end }} 
1 Like