Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jay Delisle
Jay Delisle

Posted on • Originally published atthedelisledomain.com on

     

How to Build Site Navigation with Hugo

I have been playing around with Hugo and doing research. I've come to find that there are a few different ways to build out the site navigation. This tends to be the case and differs from each static site generator. So how do we build out the site navigation with Hugo? Let's jump right into it!

There are a few different ways, but there are two main ways that I use and find the most useful. If interested in a more in-depth explanation of the different ways, check out thisyoutube video I found when figuring this out myself.

First, we'll go over the front matter option. Second, we will go over the completely customized navigation method. After explaining these two methods, we will see how to actually implement either option by looping over the menu option from either the front matter or custom navigation

Front Matter

Using the front matter on markdown pages is a quick and easy solution. This is especially nice for smaller websites that don't use an extensive amount of pages. If you want to make a markdown file be part of your navigation just add the following in the front matter of the file:

---menu: "mainmenu"---
Enter fullscreen modeExit fullscreen mode

You can name the menu anything you want. This will use thetitle attribute to name the actual navigation. Let's say you have a markdown file with the title ofAbout Me, but you want the actual navigation link to sayAbout. Then you would do the following instead of the above:

---menu:    mainmenu:        name: "About"---
Enter fullscreen modeExit fullscreen mode

This can be done anytime to allow you to determine the name of the navigation link.

Completely Custom Navigation

The above works well for a small site, but as the number of pages grows, it doesn't scale well. I tend to just use the custom navigation from the start, it flows well with how I develop and build my sites. To build the custom navigation, just go to theconfig.toml at the root of the Hugo project. Here we will add our custom menu. This will follow a similar pattern to the front matter when customizing the name. We will have ourmenu and then in that menu decide what the menu will be called -menu.nav. Againnav here can be named whatever you want to name the menu. Then for each navigation link, we will have:

  • identifier: used to identify this specific link
  • name: The name you want to appear on the site's navigation
  • url: Therelative url to the page you want the link to go to
  • weight: another way of saying the order

The above would be done for each navigation link. The below code snippet is the example code for what we talked about above, having a home page, about, and contact.

[menu]    [[menu.nav]]    identifier = "home"    name = "Home"    url = "/"    weight = 1    [[menu.nav]]    identifier = "about"    name = "About"    url = "/about/"    weight = 2    [[menu.nav]]    identifier = "connect"    name = "Connect"    url = "/connect/"    weight = 3
Enter fullscreen modeExit fullscreen mode

Implement the Navigation

Now, whether you used the front matter or custom navigation, we have to implement the navigation into thenav template file. To do so, I have anav.html which holds everything in my<nav></nav> tag and I use this as a partial - love partials!. In Hugo, we can loop over these items using therange keyword and then dictate what menu to loop over. This would look like

{{ range .Site.Menus.nav }}{{ end }}
Enter fullscreen modeExit fullscreen mode

The only thing that would be different for you possibly is again what you name your menu. I named minenav so it is.Site.Menus.nav. If you kept themainmenu from the template it would be.Site.Menus.mainmenu. Within this template block, you can now just write the normal HTML for the navigation. The final code should look similar to what I have below:

<ul>    {{ range .Site.Menus.nav }}    <li><a href=" {{ .URL }}">{{ .Name }}</a></li>    {{ end }}</ul>
Enter fullscreen modeExit fullscreen mode

I hope you were able to learn some of the options to implement navigation in a Hugo site. I encourage you again to check out the youtube video linked above which helped me at the start as well. Until next time - Stay Coding and check out my other posts on my bloghere!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Life Long Learner. Python lover. Mechanical Engineer turned Software Engineer
  • Location
    Rhode Island, USA
  • Work
    Software Automation Engineer
  • Joined

More fromJay Delisle

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp