Menus generated from config?

Currently, the order of menu items defined in the config is not respected:

menu:
  main:
    - identifier: "accueil"
    - identifier: "resources"
    - identifier: "contact"
    - identifier: "contacter"
      parent: "contact"
    - identifier: "trouver"
      parent: "contact"

That will generate:

accueil
contact
    contacter
    trouver
resources 

Is this behaviour expected? Can we do anything (apart from weight) to respect the order of items?

Also, I would appreciate a shorter notation with a children parameter like so:

menu:
  main:
    - identifier: "accueil"
    - identifier: "resources"
    - identifier: "contact"
      children:
        - identifier: "contacter"
        - identifier: "trouver"

Would anyone be interested in such a feature? Unfortunately, my go skills and my understanding of the hugo codebase are close to zero, so I think I would need help to implement something like that :slight_smile:

If the integrated menu system can allow me at least to have the order of items respected without using the weight param, this will allow me to ditch my over-engineered data-files menu system which I’ve already begun to simplify.

Note: i don’t want to set the URL here, because I do it in the translation files directly, hacking around with plurals and singulars. But I want my menus to be the same across languages.

EDIT: @kaushalmodi proposed a solution to my first question: template + explainations.

1 Like

I was bugged by that too… So I do this: https://github.com/kaushalmodi/hugo-onyx-theme/blob/master/layouts/partials/sidebar/menu.html

Can you guess the trick? :slight_smile:


Actually scratch that, that wouldn’t work for all editting flows.

Unfortunately no (i speak very little regex ^^). I don’t see what in your template would reorder the menu properly. Could you maybe explain for me please?

I embed the menu weights as part of menu names. That way, I need zero menu config in my site config.

The menu names are are “0.search”, “1.foo”, and so on. I rarely add/remove menus, so I don’t need to renumber the weights.

Then when .Site.Menus is looped, it will go through those menus in alpha(numerical) order, and I get the menu order as expected (set by the weight prefixes).

About the editing flow: In my flow, I write all posts in tree structure. So the menu name set at the tree node propagates to the children nodes. So I need to set the menu name just once. An alternative would be to have some sort of script to set the menu names.

But all of that would be a huge overkill, and setting the menu weights in the site config would eventually be better… and so I striked out that suggestion.

2 Likes

Ah, I didn’t think about this! :slight_smile:

So I guess this method would work for children entries as well… Thank you so much for your suggestion, I will try it out!

I don’t use children menus and based on the logic in that template, I’d be surprised if it would “just work”. But it should be easy to implement… once you “get” the map structure of .Site.Menus (see this for example – Search for foo" on that page, yes with that ending double-quote). In the template, you would then need to skip the menus that have .Parent defined… and if .Children is defined, you do the same looping in there… I hope that makes sense.

2 Likes

So I got around to implement it as you suggested @kaushalmodi, and it works like a charm, thank you! Here’s my template now, and my config file. It works perfectly thank you so much! :heart:

Now i guess I’m left with my second question being, can we make it less verbose? When I used data templates I used to write my menus like so:

- "accueil"
- "lieu": ["présentation", "histoire", "espaces", "participer"]
- "activités"
- "ressources": ["affiches-flyers", "photos"]
- "contact": ["contacter", "trouver"]

That was very neat. Don’t you think the config file should support a shorter notation for entries which don’t need additional params?

Should I mark this thread SOLVED and open a discussion in the feature request area? :slight_smile:

I don’t use the site config to manage menus. I just set the menu attributes from each page’s front-matter.

That said, I don’t use menus with the kind of nesting you do (my “most complex” menu is this, in the left). I can certainly see the value in what you propose if doing menu management from the site config.


As I mentioned earlier, I organize my site content in tree fashion (Org mode), so it looks like this to me:

image

From that example, the pink headings (each of which export to a separate Hugo content file) are nested under the green headings. So all pink headings under the “Enhancements” green heading will have their menu set to “6.enhancements”.

That in turn shows up on that site as:

1 Like