{{- $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}}
{{- define "menu-nav" }}
{{- $currentNode := .currentnode }}
{{- $level := .level }}
 {{- with .sect}}
  {{- if .IsSection}}
    
  
  {{- if or (.IsAncestor $currentNode) (.Params.alwaysopen) }} 
      {{- $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 }}
      
    {{- end}}
  {{- end}}
 {{- end}}
{{- end}}