I have made some more updates to the theme for this site. Fontawesome icons have been added and I will be using them while updating the styling and creating new pages. There is now a breadcrumb trail, so you can navigate to any parent pages (such as Site Updates in the case of this post).
I may make improvements to the code in the future, but for the time being, it works as desired.
The theme is available via the GitHub repository go-hugo-theme-texotela.
To create the breadcrumb trail, I first create a partial layouts/partials/breadcrumb-trail.html
:
|
|
It is then referenced in the page template layouts/_default/baseof.html
.
|
|
To prevent it showing on the home page, a check is done {{ if not .IsHome }}
before including the relevant styling and the partial with the required parameters, using the dict function. The CurrentPage
parameter is set to true
, since this is the initial iteration and the Page
parameter is set to the current page context. A recursive iteration is performed, rendering links to the parent pages before rendering the current pages title.
In the partial, a check is made to ensure the page has a parent with {{ if .Page.Parent }}
. Then it checks if the parent is the home page, if it is, the home icon by fontawesome is used and the home page linked to. If the parent isn’t the home page, the partial is included, this time with CurrentPage
set to false
and Page
set to the parent.
The next thing to be rendered is the separator, hidden from screen readers with the aria-hidden
attribute, since it is for visual purposes only.
The title for the page is then rendered, if the CurrentPage
parameter is set to true
. If CurrentPage
is false
, a link to the page (which will be an ancestor node) is rendered.
This demonstrates one way of rendering breadcrumbs. While the code is specific to Hugo, the logic used is also applicable to any situation where you need to process or display hierarchical data (such as a folder structure, or tasks with sub tasks).