Range length or last element?

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