Johno
1
Hi Hugo community.
I am new to Hugo, but wanted to know how best to limit an array I have in my markdown file.
My code is as follows:
test.md
---
title: "Test arrays"
tests: [
“test1.jpg”,
“test2.jpg”,
“test3.jpg”
]
---
index.html
{{ range .Params.tests }}
{{ . }}
{{ end }}
How can I do it so it only shows two arrays from the list so test1.jpg and test2.jpg? I tried with first 2
but didn’t work…
Any help would be much appreciated.
zwbetz
2
Try this template:
{{ $collection := first 2 .Params.tests }}
{{ range $collection }}
{{ . }}
{{ end }}
Which will output:
“test1.jpg”
“test2.jpg”
2 Likes
Johno
3
oh I have to add it in a slice?! Now I get it.
Thank you so so much @zwbetz !
Superb community.
zwbetz
4
That’s just my personal preference
You could have done it like this
{{ range first 2 .Params.tests }}
{{ . }}
{{ end }}
system
Closed
5
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.