Range length or last element?

i need to know if its the last element in a range… like:

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

$index is the actual “position”, works.
$element is like “”.

I need a

between all elements but not after the last element.


edit: ok i will take the other way with index 0… will work, but is my original question solvable?


$list := (where .Site.Pages "Section" "blablabla")
$len := (len $list)

range $index, $element := $list
if eq (add $index 1) $len

not tested, but thx! i will try it. :slightly_smiling:

Tnx, this helped me a lot.

I integrated this in the nav of my testsite and it works like a charm. I wanted to separate the link items by a pipe symbol but not the last entry.

Here’s the code:

<nav id="mainmenu">
  {{ $currentPage := . }}
  {{ $list := (.Site.Menus.main) }}
  {{ $len := (len $list) }}
    <!-- All sites shall have a link to the home page except the home page -->
    {{ if not .IsHome }}
      <li><a href="/">Home</a></li>
    {{ end }}

    {{ range $index, $element := $list }}
        {{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}
        class="active anyclass"
        {{ else }}
        {{ end }}>
          {{ if eq (add $index 1) $len }}
          {{ else }}
          class="anyclass border-right"
          {{ end }}
          href="{{ .URL | absURL }}">
          {{ .Pre }} {{ .Name }}
    {{ end }}