Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Farai Gandiya
Farai Gandiya

Posted on • Originally published atcodelab.farai.xyz on

     

Breadcrumbs In Hugo

Breadcrumbs In Hugo was first published onFarai's Codelab.


Breadcrumbs are a type of navigation which shows pages in a particular order, like viewing history or, in my case, a page’s hierarchy.

Links to home, the old ones and v8 redesign with a right arrow between them

Implementing this was frustrating, but I eventually figured it out. In short, you need to do something similar to this:

<!-- In whatever template --><p>    {{ partial "breadcrumbs.html" . }}</p>
Enter fullscreen modeExit fullscreen mode

Then inbreadcrumbs.html, include this:

{{ with .Parent }}    {{ partial "breadcrumbs.html" . }}    {{ if .Parent }}→{{ end }}    <a href="{{ .Permalink }}">{{ .Title }}</a>{{ end }}
Enter fullscreen modeExit fullscreen mode

The important thing is that you:

  1. invoke the recursion by calling the breadcrumbs partial to the.Parent.
  2. provide the separator (given you have one) conditional to whether the current.Parent has a parent and
  3. link to the current crumb.

That’s it.

You can play with semantics based on what you need. Want to usenav > ul instead ofp and make them list items so you can useBootstrap’s breadcrumbs? Go ahead.

I spent far more time than I should have trying to solve this and this is what I ended up with. If you want the full story on how I got to this point, it’s aPatreon Exclusive. Thanks tonfriedli for figuring this out!!


Thanks for reading! If you liked this post, considersupporting my work by:

You can alsosubscribe to my newsletter.

Have feedback? Send an email togandiyafarai+feedback at gmail dot com

Top comments(1)

Subscribe
pic
Create template

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

Dismiss
CollapseExpand
 
integr8or profile image
Mark Stouffer
Full stack developer of the .Net veriety. Also, significant exposure to Next.js, Rust and some alternate tools.
  • Location
    Brea, CA
  • Work
    Full stack dev work
  • Joined

I spent way too much time trying to concatinate a string in Hugo yesterday to build the path before I found this recursive fix, which works perfectly. Thanks.

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

¯\_(ツ)_/¯
  • Location
    Harare, Zimbabwe
  • Education
    B.A in Computer Science from Wartburg College
  • Joined

More fromFarai Gandiya

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