[SOLVED] How do I (for) loop in a html template?

Hi,
I wonder what is the nicest way to emulate for loop in html template. Sorry to ask such a basic question, but google search doesn’t help in this case. Thanks!

for i = 0; i < 5; i++ {
   ...
}
1 Like

Go templates has no traditional for loop.

You have to range over a list.

The closest thing in Hugo:

{{ range $i, $sequence :=  (seq 5) }}
{{ $i }}: {{ $sequence }}
{{ end }}

The above should print:

0: 1
1: 2
2: 3
3: 4
4: 5
8 Likes

This works great, thanks!

Is the seq function documented somewhere?

https://hugodocs.info/functions/seq/

where it should be use? in script or in head?plzzzz ans ASAP

Thanks for the same. It works for me.

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