Hugo NextInSection with counter

need to accomplish a layout with a link to next and previous post and a counter marking the actual post, bad representation ahead:

<previousPost 10/100 nextPost>

I put everything to work, except the mention of the current post number:

{{ $posts := (where .Site.RegularPages "Section" "==" "news") }}
{{ $postCount := len $posts }}

{{ if .PrevInSection }}
  <a href="{{.PrevInSection.Permalink}}">Prev Post</a>
{{ end }}

{{ I have no idea }}/{{ $postCount }}

{{ if .NextInSection }}
  <a href="{{.NextInSection.Permalink}}">Next Post</a>
{{ end }}

And I have no clue on how to find the value of the page in the netxInSection context. I’m thinking about changing my code to a range and use the index to mark the current page but I think that should be a smarter way.

Thanks!

After some time I found a way to do it…

      {{ range $index, $element := (where .Site.RegularPages "Type" "news" ).Reverse }}
        {{ if eq . $ }}
          {{- $.Scratch.Set "currentItem" (add $index 1) }}
        {{ end }}
      {{end}}

      {{ $posts := (where .Site.RegularPages "Section" "==" "news") }}
      {{ $postCount := len $posts }}

      {{ if .PrevInSection }}
        <a href="{{.PrevInSection.Permalink}}">prev</a>
      {{ else }}
        <a href="" disabled>prev</a>
      {{ end }}

      {{ $.Scratch.Get "currentItem" }}/{{ $postCount }}

      {{ if .NextInSection }}
        <a href="{{.NextInSection.Permalink}}">next</a>
      {{ else }}
        <a href="" disabled>next</a>
      {{ end }}

Not sure if the best way, but works!

1 Like

Thanks @eduruiz - you’ve saved me some work. I think this should be in the range documentation :slight_smile:

Yes, please add it.:+1:

2 Likes

Happy to. Looking at the range page, it’s pretty sparse. Do you think there’s a more appropriate place for it? I think logically I’d pair it with len as I can imagine a major use case would be a variation of:

You are viewing page Index of Total

Or maybe even a progress meter (ie: a percentage of the two) - my use-case is both.

What do you think?