Menu items with description from front matter

Hi all, I’m trying to generate a menu where the menu items have a header and a subsection:

36

I’d like to build the menu from the front matter of the pages. Here is an example:

---
menu:
  guides:
    weight: 20
title: Tutorial
description: Build a small application the BDD way
---

Using {{ range .Site.Menus.guides }} I can access the page title with {{ .Name }}, but how would I access the description?

Thanks in advance,
Aslak

1 Like

There is no reference to a page from a Menu Entry itself but I think you could find your way back with a page lookup. You would have to play with the Identifier or some other Menu Entry value to link the page from the outside-in.

Maybe, with something like this (untested):

{{ range .Site.Menus.guides }}
    {{ .Name }}
    {{ with index (where .Site.Pages "pageMenus.guides.Identifier" "some-id") 0  }}
      {{ .Params.description }}
    {{ end }}
{{ end }}

And

---
menu:
  guides:
    identifier: some-id
    weight: 20
title: Tutorial
description: Build a small application the BDD way
---

I “think” you could also abuse the menu system a bit by using pre or post to hold that data and print it in your template.

1 Like

I’ve abused pre and post that way, and it worked.

2 Likes

@budparr and @RickCogley, can you folks link to examples? I can’t figure out what pre and post do from the docs. I’d like to know how it is “abused”. :slight_smile:

1 Like

Well, my example won’t be so relevant for you perhaps, but you can set them how you want and just refer to them. This uses .post to set target=_blank when I wanted to open the link in another window.

Huh, okay.

:thinking:

Why are they called post and pre? Looks like you can put them anywhere.

I like knowing the “why” of things, it helps me remember what stuff does. :slight_smile:

SOLUTION:

Let’s say you have in your .md file the metadata:

author: Bob Dole
menu: "guest"

Your menu can access the author if you use {{ with .Page }} and {{ .Params.author }}

{{ range .Site.Menus.guest }}
  <div>
    {{ with .Page }}
      {{ .Name }} - {{ .Params.author}}
    {{ end }}
  </div>
{{ end }}

Cheers!