{{- $currentNode := . }}
{{- if eq .Site.Params.ordersectionsby "title"}}
  {{- range .Site.Home.Sections.ByTitle}}
  {{- template "menu-nav" dict "sect" . "currentnode" $currentNode "level" 1}}
  {{- end}}
{{- else}}
  {{- range .Site.Home.Sections.ByWeight}}
  {{- template "menu-nav" dict "sect" . "currentnode" $currentNode "level" 1}}
  {{- end}}
{{- end}}

<!-- templates -->
{{- define "menu-nav" }}
{{- $currentNode := .currentnode }}
{{- $level := .level }}
 {{- with .sect}}
  {{- if .IsSection}}
    <option value="{{ .RelPermalink}}" {{if eq .Permalink $currentNode.Permalink}} selected{{end}}>
  {{if gt $level 1 }}
    {{- range after 1 (seq $level)}}-{{ end }} 
  {{end}} {{.Title}}</option>
  
  {{- if or (.IsAncestor $currentNode) (.Params.alwaysopen) }} <!-- dig into sub levels only if active or menu is always ON-->

      {{- $numberOfPages := (add (len .Pages) (len .Sections)) }}
      {{- if ne $numberOfPages 0 }}
          {{- .Scratch.Set "pages" .Pages }}
          {{- if .Sections}}
          {{- .Scratch.Set "pages" (.Pages | union .Sections) }}
          {{- end}}
          {{- $pages := (.Scratch.Get "pages") }}

        {{- if eq .Site.Params.ordersectionsby "title"}}
          {{- range $pages.ByTitle }}
            {{- if and .Params.hidden (not $.showhidden) }}
            {{- else}}
            {{- template "menu-nav" dict "sect" . "currentnode" $currentNode  }}
            {{- end}}
          {{- end}}
        {{- else}}
          {{- range $pages.ByWeight }}
            {{- if and .Params.hidden (not $.showhidden) }}
            {{- else}}
            {{- template "menu-nav" dict "sect" . "currentnode" $currentNode "level" (add $level 1) }}
            {{- end}}
          {{- end}}
        {{- end}}

      {{- end}}
  {{end}}
  {{- else}}
    {{- if not .Params.Hidden }}
      <option value="{{ .RelPermalink}}" {{if eq .Permalink $currentNode.Permalink}} selected{{end}}> 
{{- range after 1 (seq $level)}}-{{ end }} {{.Title}}</option>
    {{- end}}
  {{- end}}
 {{- end}}
{{- end}}
