I’d like to range over a slice from an array within a template.
Hugo offers this nice function:
{{ range first 10 array }}
<!-- range [2:10] array -->
{{ end }}
Instead of retrieving all ten items of the array I wanted to retrieve second till tenth [2:10]
Is there a way in hugo?
thanks in advance
bep
2
I think you currently have to add a conditional for the index (range $index, $element …) inside the loop.
I did, like this example shows:
{{ range $index, $element := .Params.slideshow }}
{{ if $index eq 1 }}
{{ $element.head_title }}
{{ else }}
{{ $element.title }}
{{ end }}
{{ end }}
But this produces an error:
can't give argument to non-function $index
Any hints?
thanks in advance
spf13
5
That’s not how go templates order function calls. You need to change around the order of that if statement. There’s examples in the docs.
Steve Franciaspf13.com@spf13
Excuse me! Read the docs first
Now everything is working as expected.
Thanks @bjornerik and @spf13 for the quick and helpful support.
By the way: Hugo is great!
Cheers
1 Like