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.

:confused:

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

Pseudo:

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

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

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) }}
  <ul>
    <!-- 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 }}
      <li
        {{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}
        class="active anyclass"
        {{ else }}
        class="anyclass"
        {{ end }}>
        <a
          {{ if eq (add $index 1) $len }}
          class="anyclass"
          {{ else }}
          class="anyclass border-right"
          {{ end }}
          href="{{ .URL | absURL }}">
          {{ .Pre }} {{ .Name }}
        </a>
      </li>
    {{ end }}
  </ul>
</nav>
1 Like