Custom menu for pages

Almoststatic is a very versatile system.

With it you can use a different menu for each page, and this can ne useful to separate various parts of the site or even to write multilingual sites.

Since with Almoststatic you can configure everything and write your widgets, there are many ways to do that.

But I think that the simplest way is the one used in this site where all blog pages uses a custom menu. To do this I did so:

  • First of all I added a menu called "BlogMenu" in the config.yaml file.
  • Then I added the field menu: BlogMenu to each page who needed that.
  • And then in the navbar.html widget I added the Jinja2 directive {% for item in config[page.menu] or MainMenu -%} to iterate the menu.

Done!

Note that Almoststatic add to the Jinja2 globals environment variable the field config with the whole content of the config.yaml file so we can access to the custom menu with config[page.menu] and if not present the default MainMenu is used.