Getting next and previous resource in slice

Hello,
I’m iterating through collection of resources and I somehow need to get next and previous resource in the slice. Is there any way to achieve that? I know that there are NextInSection and PrevInSection for section pages, but I know of no analogy for page resources.

<ul>
{{ range .Resources.Match "images/*.jpeg" }}
  <li id="{{ .Title }}">
    <a href="#{{/* Title of the previous resource */}}">Previous</a>
    {{ .Title }}
    <a href="#{{/* Title of the next resource */}}">Next</a>
  </li>
{{ end }}
</ul>

Any ideas?

Something like this.

{{ $collection := .Resources.Match "images/*.jpeg" }}
<ul>
{{ range $i, $res := $collection }}
<li id="{{ .Title }}">
    {{ if gt $i 0}}
     <a href="#{{ (index $collection (sub $i 1)).Title }}">Previous</a>
    {{ end }}
  
    {{ .Title }}
    
    {{ if lt (add $i 1) (len $collection) }}
     <a href="#{{ (index $collection (add $i 1)).Title }}">Next</a>
    {{ end }}
</li>    
{{ end }}
</ul>
1 Like

This works perfectly, thanks! I could figure this out, but since I’m totally new to Hugo (and Go), I was not exactly sure about the syntax or whether there is some more expressive way to achieve this.

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