[Solved] How to print .Site.Menus for debug?

Hello,

From menu.go:

// MenuEntry represents a menu item defined in either Page front matter
// or in the site config.
type MenuEntry struct {
	URL        string
	Page       *Page
	Name       string
	Menu       string
	Identifier string
	title      string
	Pre        template.HTML
	Post       template.HTML
	Weight     int
	Parent     string
	Children   Menu
}

// Menu is a collection of menu entries.
type Menu []*MenuEntry

// Menus is a dictionary of menus.
type Menus map[string]*Menu

I see that Menus is a map and MenuEntry is a struct, but I don’t understand what []* is…

  • Doing {{ range $i, $j := .. }} over .Site.Menus works
    • Each of the $j elements in that range would be of type Menu.
      • But I am lost how to loop through each item in Menu collection… range does not work.
        • Once above step is solved, I can easily print the MenuEntry struct elements.

Trying to range through Menu gives errors like:

ERROR 2018/02/05 23:38:11 Error while rendering "home" in "": template: /home/kmodi/stow/pub_dotfiles/emacs/dot-emacs.d/elisp/ox-hugo/doc/themes/hugo-alabaster-theme/layouts/index.html:20:8: executing "/home/kmodi/stow/pub_dotfiles/emacs/dot-emacs.d/elisp/ox-hugo/doc/themes/hugo-alabaster-theme/layouts/index.html" at <partial "sidebar/sid...>: error calling partial: template: theme/partials/sidebar/sidebar.html:18:11: executing "theme/partials/sidebar/sidebar.html" at <partial "sidebar/men...>: error calling partial: template: theme/partials/sidebar/menu.html:63:3: executing "theme/partials/sidebar/menu.html" at <partial "debugprint....>: error calling partial: template: partials/debugprint.html:38:11: executing "partials/debugprint.html" at <partial "debugprint....>: error calling partial: template: partials/debugprint.html:38:11: executing "partials/debugprint.html" at <partial "debugprint....>: error calling partial: template: partials/debugprint.html:37:13: executing "partials/debugprint.html" at <$value>: range can't iterate over {/doc/why-ox-hugo/ Page("Why ox-hugo?") Why ox-hugo? early_questions why-ox-hugo    3001  []}

I am using this debugprint.html partial.

You can recreate the above error on any site with menus by doing below after copying the above linked partial and putting below anywhere in your layout:

{{ partial "debugprint.html" $.Site.Menus }}

I’d like some help here… here’s my sandbox site repo, where I reproduce the failure in the menu-debug-error branch.

To reproduce the error yourself:

  1. Clone that repo.
  2. Check out the menu-debug-error branch.
  3. Run hugo.

Here’s the fail log:

11:22:57 AM: ERROR 2018/02/06 16:22:57 Error while rendering "home" in "": template: /opt/build/repo/layouts/index.html:66:7: executing "main" at <partial "debugprint....>: error calling partial: template: theme/partials/debugprint.html:41:11: executing "theme/partials/debugprint.html" at <partial "debugprint....>: error calling partial: template: theme/partials/debugprint.html:41:11: executing "theme/partials/debugprint.html" at <partial "debugprint....>: error calling partial: template: theme/partials/debugprint.html:40:13: executing "theme/partials/debugprint.html" at <$value>: range can't iterate over {/posts/hugo-menu-as-keyword--post-with-menu-1/ Page("Post with menu 1 (HUGO_MENU as keyword)") Post with menu 1 (HUGO_MENU as keyword) auto weight post-with-menu-1--hugo-menu-as-keyword 1001 []}

The line responsible is this that tries to print debug info about .Site.Menus recursively. Commenting that out will get rid of the error.

I am looking for the correct syntax to be used in debugprint.html to successfully recurse .Site.Menus.

Oh well, sorry for the noise… it was the case of missing $.

Now this works awesome!

{{ partial "debugprint.html" .Site.Menus }} 

For reference, this is what the Menu debug looks like: https://hugo-sandbox.netlify.com/#debug-menu

1 Like