๐งญ Navigation Menus
Last updated๏ผ2026-11-25
๐งญ Navigation Menu
Header and footer navigation menu data. Admin path: Admin โ Content Management โ Navigation Management
๐ Available Fields
Each navigation object provides the following fields; {{range .Navigations}} iterates all navigation items, then use .Location to distinguish header/footer.
- {{.Name}} Menu name โ text
- {{.Link}} Link address (site prefix added automatically) โ URL
- {{.Target}} Open behavior (_blank = new window, empty = current page) โ text
- {{.Location}} Position (header = top, footer = bottom) โ text
- {{.ParentID}} Parent ID (0/empty = top level, used for multi-level menus) โ number
- {{.Sort}} Sort order (smaller numbers come first) โ number
๐ป Usage Example: Header Navigation (with current-page highlight)
Navigation objects do not have an Identifier field. For current-page highlighting, prefer the server-side {{.CurrentNav}} (see Global Helper Variables): it is injected site-wide, works on the home page / list pages / category pages / detail pages, and takes one of the values home / about / contact / products / news / cases / jobs / error. Map CurrentNav to the first path segment of the navigation link; on a match, add the is-current class and set aria-current="page".
<!-- Recommended: server-side CurrentNav highlight (works site-wide, incl. home/list pages) -->
{{$navKey := ""}}
{{if eq .CurrentNav "products"}}{{$navKey = "/products"}}
{{else if eq .CurrentNav "news"}}{{$navKey = "/news"}}
{{else if eq .CurrentNav "cases"}}{{$navKey = "/cases"}}
{{else if eq .CurrentNav "about"}}{{$navKey = "/about"}}
{{else if eq .CurrentNav "contact"}}{{$navKey = "/contact"}}
{{else if eq .CurrentNav "jobs"}}{{$navKey = "/jobs"}}
{{end}}
<nav>
{{range .Navigations}}
{{if eq .Location "header"}}
<a href="{{.Link}}"{{if and $navKey (eq .Link $navKey)}} class="is-current" aria-current="page"{{end}}{{if .Target}} target="{{.Target}}"{{end}}>{{.Name}}</a>
{{end}}
{{end}}
</nav>
โ ๏ธ Pitfall: Navigation Text Contrast (white text invisible on light backgrounds)
The home page usually has a dark hero, so white navigation text is fine; but on inner pages with lighter backgrounds, white text becomes invisible. Dynamically add a class to `based onCurrentNav`, then control the navigation text color with CSS:
<body{{if eq .CurrentNav "home"}}{{else if eq .CurrentNav "contact"}} class="inner-page inner-page--dark"{{else if eq .CurrentNav "error"}} class="inner-page inner-page--dark"{{else}} class="inner-page"{{end}}>
.inner-page โ navigation text turns dark (#1c2922); .inner-page--dark (contact / error with dark green backgrounds) โ keep white text. After changing the template you must restart the service (the front end has a 1-minute in-process page cache plus already-generated htm/ static pages; after changes, click “Update Cache” once for immediate effect).
<!-- Footer navigation -->
<footer>
{{range .Navigations}}
{{if eq .Location "footer"}}
<a href="{{.Link}}" target="{{.Target}}">{{.Name}}</a>
{{end}}
{{end}}
</footer>
<!-- With language switcher + contrast body class (complete header.html snippet) -->
<body{{if eq .CurrentNav "home"}}{{else if eq .CurrentNav "contact"}} class="inner-page inner-page--dark"{{else if eq .CurrentNav "error"}} class="inner-page inner-page--dark"{{else}} class="inner-page"{{end}}>
<nav>
{{range .Navigations}}
{{if eq .Location "header"}}
<a href="{{.Link}}"{{if and $navKey (eq .Link $navKey)}} class="is-current" aria-current="page"{{end}}{{if .Target}} target="{{.Target}}"{{end}}>{{.Name}}</a>
{{end}}
{{end}}
</nav>
{{if .LangSwitcher}}
<div class="lang-switch">
{{range .LangSwitcher}}
<a href="{{.URL}}" class="lang-item{{if .IsCurrent}} active{{end}}">{{if .Name}}{{.Name}}{{else}}{{.Code}}{{end}}</a>
{{end}}
</div>
{{end}}
๐ Alternative (sub-path prefix matching / multilingual subsites)
If you need sub-paths to also highlight the parent item (e.g. /news/123 highlights “Growth Notes”), or when building multilingual subsites where the server-side CurrentNav mapping falls short, you can use JS to prefix-match a[href] against location.pathname:
<script>
document.querySelectorAll('.main-nav a').forEach(function (a) {
var href = a.getAttribute('href');
if (href !== '/' && location.pathname.indexOf(href) === 0) {
a.classList.add('is-current'); a.setAttribute('aria-current', 'page');
}
});
</script>
The legacy form {{if eq $.CurrentPath .Link}}active{{end}} also works, but CurrentPath is only injected on detail pages / category pages (empty on the home page and list pages), so highlighting fails on those two pages; for new projects, use CurrentNav directly.
๐ Real Site Navigation Example (gitl.cn)
Taking gitl.cn (Tiantian Technology) as an example, the real menu structure (header position) from the admin “Navigation Management” is as follows; iterate with {{range .Navigations}} and render by .Location:
| Menu Name (.Name) | Link (.Link) | Position |
|---|---|---|
| Home | / | header |
| Real Cases | /cases | header |
| Growth Notes | /news | header |
| Contact Us | /contacts | header |
[โ Site Info](04-site.html) [Slideshow Carousel โ](06-banner.html)
CloudSite SaaS