How do I insert an element on every iteration of a range EXCEPT the last?

{{ define "main" }}
<main id="site-content">
  {{ $paginator := .Paginate (where .Site.RegularPages "Type" "blog") }}
  {{ range $paginator.Pages }}
  <article>
    <header>
      <h2 class><a href="{{ .Permalink }}">{{ .Title }}</a></h2>
      <time>{{ .Date.Format "January 2, 2006" }}</time>
    </header>
  </article>
  /* INSERT <HR> HERE EXCEPT FOR LAST IN RANGE */
  {{ end }}
  {{ template "partials/pagination.html" . }}
</main>
{{ end }}

I’m trying to insert a horizontal divider on every block in a range EXCEPT the last one. Something like {{ if ne $something }}<hr>{{ end }}.

But I don’t know what that $something might be or how to go about setting that up! Any help would be greatly appreciated.

EDIT #2: Sorry to edit this a second time, but it does appear to actually be broken still. It is showing the pagination numbers now, but each page shows ALL posts… sort of defeating the purpose of pagination. So I would say its broken as appears below. I’ll work on it some more and try to have a working case for future reference. If anyone can help I’d greatly appreciate it. If its not fixable, I hope it will be of some minimal help to someone in the future too as a starting point.

EDIT: OK, it DOES work! I forgot to change the pagination to something low enough in which it would appear, and everything appears to be working as intended. Left here below in case someone else needs it in the future:

{{ define "main" }}
{{ $list:= (where .Site.RegularPages "Type" "blog")  }}
{{ $len := (len $list) }}
<main id="site-content">
  {{ $paginator := .Paginate ($list) }}
  {{ range $index, $element := $list }}
  <article>
    <header>
      <h2 class><a href="{{ .Permalink }}">{{ .Title }}</a></h2>
      <time>{{ .Date.Format "January 2, 2006" }}</time>
    </header>
  </article>
    {{ if ne (add $index 1) $len }}
    <hr>
    {{ end }}
  {{ end }}
  {{ template "partials/pagination.html" . }}
</main>
{{ end }}

Hi, thanks! So, this does work for the purposes I’m looking for, but sadly it now also breaks the pagination that I fixed in the previous thread.

Anyway I can have both work at the same time? If I do {{ $paginator := .Paginate ($list.Pages) }} it seems to break the site somehow whereas without the above changes the pagination previously worked flawlessly.

I’m getting closer! I hope I can get both the conditional <hr> to work at the same time pagination is also working too.

It apears as if the $index is being reset for EACH page, as in the index = 0 at the start of each page which as far as I can tell makes the above example probably not the best route.

Unless I’m missing something that is not apparent to me.

This leaves me back to the drawing board for ideas: how can I put an <hr> after each element in a range EXCEPT the last one (on each page). It has indeed become a bit more complex of a task than I had initially anticipated…

Untested, but something like:


  {{ $paginator := .Paginate (where .Site.RegularPages "Type" "blog") }}
  {{ $len := len $paginator.Pages }}

  {{ range $i, $e :=  $paginator.Pages }}
  ... 
    {{ if ne (add $i 1) $len }}
    <hr>
    {{ end }}

  {{ end }}
  {{ template "partials/pagination.html" . }}
</main>

You get the len of the Pages in the current range, not the list of pages to be paginated.

1 Like

Ah, I see! The above code I tested and it works perfectly. My gratitude to you.

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