Can I get the current iteration from `{{ range .Pages }}`?

I need to set a id to an incremented value, is there any way I can get the current count that I’m on OR increment my own value each loop?

Currently my way of doing it is:
{{ $count := 0 }}
{{ range .Pages }}
{{ $count = add $count 1 }}

{{ range $iteration, $value := .Pages }}
{{ $value }}
{{ $iteration }}
{{ end }}

$value is what the dot . was before, $iteration should increase starting from zero. But your way is right too.

On a related note: ID as such, if you want to have a real unique identification method for example an article - try this:

<article{{ with .File }} id="x{{ .UniqueID }}"{{ end }}>
1 Like