Limit array loop?

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.

Try this template:

{{ $collection := first 2 .Params.tests }}

{{ range $collection }}
  {{ . }}
{{ end }}

Which will output:

“test1.jpg”
“test2.jpg”
2 Likes

oh I have to add it in a slice?! Now I get it.
Thank you so so much @zwbetz !

Superb community. :smile:

That’s just my personal preference :slightly_smiling_face:

You could have done it like this

{{ range first 2 .Params.tests }}
  {{ . }}
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.