Range with an index counter

This took me some time before finally running across a working sample, so I will share how to range an array (e.g. .columns) with an index (counter):

    {{range $index, $element := .columns}}
        index:{{ $index }} 
        name:{{ $element.name }} 
    {{ end }}

reference: https://knausb.github.io/2014/04/hugo-template-primer/
If someone can explain why I need to create a variable (e.g. $element) please do.

17 Likes