menu.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. {{- /*
  2. Renders a menu for the given menu ID.
  3. @context {page} page The current page.
  4. @context {string} menuID The menu ID.
  5. @example: {{ partial "menu.html" (dict "menuID" "main" "page" .) }}
  6. */}}
  7. {{- $page := .page }}
  8. {{- $menuID := .menuID }}
  9. {{- with index site.Menus $menuID }}
  10. <nav>
  11. <ul>
  12. {{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
  13. </ul>
  14. </nav>
  15. {{- end }}
  16. {{- define "_partials/inline/menu/walk.html" }}
  17. {{- $page := .page }}
  18. {{- range .menuEntries }}
  19. {{- $attrs := dict "href" .URL }}
  20. {{- if $page.IsMenuCurrent .Menu . }}
  21. {{- $attrs = merge $attrs (dict "class" "active" "aria-current" "page") }}
  22. {{- else if $page.HasMenuCurrent .Menu .}}
  23. {{- $attrs = merge $attrs (dict "class" "ancestor" "aria-current" "true") }}
  24. {{- end }}
  25. {{- $name := .Name }}
  26. {{- with .Identifier }}
  27. {{- with T . }}
  28. {{- $name = . }}
  29. {{- end }}
  30. {{- end }}
  31. <li>
  32. <a
  33. {{- range $k, $v := $attrs }}
  34. {{- with $v }}
  35. {{- printf " %s=%q" $k $v | safeHTMLAttr }}
  36. {{- end }}
  37. {{- end -}}
  38. >{{ $name }}</a>
  39. {{- with .Children }}
  40. <ul>
  41. {{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
  42. </ul>
  43. {{- end }}
  44. </li>
  45. {{- end }}
  46. {{- end }}