ef9c4913c Clean up and removal of outdated examples 46122c9aa add godot tutorials to showcase 06d1d1ea2 Update scss-sass.md 1fc63c100 Spelling fix in 0.79.1 release notes ad2f50e3d Update plainwords description (#1296) 33021d451 Update substr examples (#1304) 6b1cc59bb Release 0.80.0 521db8c6d Merge branch 'tempv0.80.0' 58626c2b3 releaser: Add release notes to /docs for release of 0.80.0 f81d118af dartsass: Dart Sass only supports `expanded` and `compressed` 7da6f54be Add Dart Sass support b1f2661bb Replace jsconfig.js with jsconfig.json 38de0c1a4 Update index.md 223ceae80 Update index.md f7ac0e59d Release v0.79.1 2d4583d43 Merge branch 'temp791-2' 1d34e609b releaser: Add release notes to /docs for release of 0.79.1 e26769988 Merge branch 'temp791' 75694d904 Fix Resource.ResourceType so it always returns MIME's main type 0f65d7783 Typo s/adds/add (#1298) 0b896b2c0 images: Add images.Overlay filter 0d4257dcd Clarify documentation on shimming fcf601ddf Update index.html 6bf9bc1c1 Update index.html 1ce76bf3a Update index.html e7d976eec Update index.html db2996e64 Update index.html 245e5bfc9 news: Add post about Apple M1 3ad4115ed tpl: Add title parameter to YouTube shortcode 76ed976f8 Added two useful extensions to the list (#1243) e5a30dd11 Update related.md 25cf8f48b Improve substr examples e16e57e9a Update path.Split.md 2749b88fd Update path.Split.md d76cad3ff Release 0.79.0 f5ccfbe98 releaser: Add release notes to /docs for release of 0.79.0 ebf1b87b0 Merge commit '9f1265fde4b9ef186148337c99f08601633b6056' 1f1e8f39c Allow setting the delimiter used for setting config via OS env, e.g. HUGO_ e9b1414dd deps: Update to github.com/evanw/esbuild 0.8.11 to 0.8.14 0f76cf66c docs: Regen docshelper 1ada5d47e Add menu params 1c120aef0 Revert "docs: Regenerate docshelper" 7b60b5624 docs: Regenerate docshelper git-subtree-dir: docs git-subtree-split: ef9c4913cdcf95d62ec12d872f412f97e55a55ad
5.4 KiB
title | linktitle | description | date | publishdate | lastmod | categories | keywords | menu | weight | sections_weight | draft | aliases | toc | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Menu Templates | Menu Templates | Menus are a powerful but simple feature for content management but can be easily manipulated in your templates to meet your design needs. | 2017-02-01 | 2017-02-01 | 2017-02-01 |
|
|
|
130 | 130 | false |
|
false |
Hugo makes no assumptions about how your rendered HTML will be structured. Instead, it provides all of the functions you will need to be able to build your menu however you want.
The following is an example:
{{< code file="layouts/partials/sidebar.html" download="sidebar.html" >}}
{{< /code >}}{{% note "absLangURL
and relLangURL
" %}}
Use the absLangURL
or relLangURL
functions if your theme makes use of the multilingual feature. In contrast to absURL
and relURL
, these two functions add the correct language prefix to the url.
{{% /note %}}
Section Menu for Lazy Bloggers
To enable this menu, configure sectionPagesMenu
in your site config
:
sectionPagesMenu = "main"
The menu name can be anything, but take a note of what it is.
This will create a menu with all the sections as menu items and all the sections' pages as "shadow-members". The shadow implies that the pages isn't represented by a menu-item themselves, but this enables you to create a top-level menu like this:
<nav class="sidebar-nav">
{{ $currentPage := . }}
{{ range .Site.Menus.main }}
<a class="sidebar-nav-item{{if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} active{{end}}" href="{{ .URL }}" title="{{ .Title }}">{{ .Name }}</a>
{{ end }}
</nav>
In the above, the menu item is marked as active if on the current section's list page or on a page in that section.
Site Config menus
The above is all that's needed. But if you want custom menu items, e.g. changing weight, name, or link title attribute, you can define them manually in the site config file:
{{< code-toggle file="config" >}} menu.main name = "This is the blog section" title = "blog section" weight = -110 identifier = "blog" url = "/blog/" {{</ code-toggle >}}
{{% note %}}
The identifier
must match the section name.
{{% /note %}}
Menu Entries from the Page's front matter
It's also possible to create menu entries from the page (i.e. the .md
-file).
Here is a yaml
example:
---
title: Menu Templates
linktitle: Menu Templates
menu:
docs:
title: "how to use menus in templates"
parent: "templates"
weight: 130
---
...
{{% note %}}
You can define more than one menu. It also doesn't have to be a complex value,
menu
can also be a string, an array of strings, or an array of complex values
like in the example above.
{{% /note %}}
Using .Page in Menus
If you use the front matter method of defining menu entries, you'll get access to the .Page
variable.
This allows to use every variable that's reachable from the page variable.
This variable is only set when the menu entry is defined in the page's front matter.
Menu entries from the site config don't know anything about .Page
.
That's why you have to use the go template's with
keyword or something similar in your templating language.
Here's an example:
<nav class="sidebar-nav">
{{ range .Site.Menus.main }}
<a href="{{ .URL }}" title="{{ .Title }}">
{{- .Name -}}
{{- with .Page -}}
<span class="date">
{{- dateFormat " (2006-01-02)" .Date -}}
</span>
{{- end -}}
</a>
{{ end }}
</nav>
Using .Params in Menus
User-defined content on menu items are accessible via .Params
.
Here's an example:
<nav class="sidebar-nav">
{{ range .Site.Menus.main }}
<a href="{{ .URL }}" title="{{ .Title }}" class="{{ with .Params.class }}{{ . }}{{ end }}">
{{- .Name -}}
</a>
{{ end }}
</nav>
{{% note %}} With Menu-level .Params they can easily exist on one menu item but not another. It's recommended to access them gracefully using the with function. {{% /note %}}