Attaching an index to a {{ range }}

Hi all.

I’m making a material design theme for hugo. On the frontpage, I want to render the most recent post with a certain template, and the 2nd to 10th most recent posts with a different template.

So far I have:

{{ range first 10 (where .Site.Pages "Section" "post") }}  
        {{ .Render "card" }}
{{ end }} 

How can I modify the above code to accomplish this? Is there a way to attach an index so I could do something like:

{{ range first 10 (where .Site.Pages "Section" "post") }}
    {{if eq index 1}}
        {{ .Render "card1" }}
    {{else}}
        {{ .Render "card2" }}
    {{end}}
{{ end }} 

I’m new to Go, and haven’t had much luck googling around. Any help is appreciated!

Something like this:

{{ $index, $element := range first 10 (where .Site.Pages "Section" "post") }}

Note that the $index is zero-based.

1 Like

Ah, thanks Bep. I had tried attaching $index previously, but I must of used wrong syntax.

I got it to work using the following snippet:

{{ range $index, $element := first .Site.Params.frontpageposts (where .Site.Pages "Section" "post") }} 
    {{ if (eq $index 0) }}
        {{ .Render "card-8" }}
    {{ else }}
        {{ .Render "card-12" }}
    {{ end }} 
{{ end }}

A post was split to a new topic: Using index with range