diff options
author | Bjørn Erik Pedersen <[email protected]> | 2024-05-29 13:23:36 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-05-30 09:53:48 +0200 |
commit | f8aefd7883151acb7e5f7b71075049d57085c3ce (patch) | |
tree | 7851ee107765b1e93cc5c069bbb3926c3fc827f1 | |
parent | 98503acd31070ea35810fbe32705b430c6f26098 (diff) | |
download | hugo-fix/misccontenttempl-2024-05-29.tar.gz hugo-fix/misccontenttempl-2024-05-29.zip |
content adapter: Add support for menus in AddPagefix/misccontenttempl-2024-05-29
Fixes #12507
-rw-r--r-- | hugolib/page__menus.go | 16 | ||||
-rw-r--r-- | hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go | 28 | ||||
-rw-r--r-- | navigation/pagemenus.go | 14 | ||||
-rw-r--r-- | resources/page/pagemeta/page_frontmatter.go | 1 |
4 files changed, 45 insertions, 14 deletions
diff --git a/hugolib/page__menus.go b/hugolib/page__menus.go index 5bed2bc03..1666036ce 100644 --- a/hugolib/page__menus.go +++ b/hugolib/page__menus.go @@ -62,8 +62,22 @@ func (p *pageMenus) init() { p.p, ) + params := p.p.Params() + + var menus any + var ok bool + + if p.p.m.pageConfig.Menus != nil { + menus = p.p.m.pageConfig.Menus + } else { + menus, ok = params["menus"] + if !ok { + menus = params["menu"] + } + } + var err error - p.pm, err = navigation.PageMenusFromPage(p.p) + p.pm, err = navigation.PageMenusFromPage(menus, p.p) if err != nil { p.p.s.Log.Errorln(p.p.wrapError(err)) } diff --git a/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go b/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go index c7e3d96c7..3a37c233b 100644 --- a/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go +++ b/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go @@ -610,3 +610,31 @@ foo b.AssertFileContent("public/a/index.html", "|xfoo|") b.AssertFileContent("public/b/index.html", "|foo|") // fails } + +func TestPagesFromGoTmplMenus(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +disableKinds = ['rss','section','sitemap','taxonomy','term'] + +[menus] +[[menus.main]] +name = "Main" +[[menus.footer]] +name = "Footer" +-- content/_content.gotmpl -- +{{ .AddPage (dict "path" "p1" "title" "p1" "menus" "main" ) }} +{{ .AddPage (dict "path" "p2" "title" "p2" "menus" (slice "main" "footer")) }} +-- layouts/index.html -- +Main: {{ range index site.Menus.main }}{{ .Name }}|{{ end }}| +Footer: {{ range index site.Menus.footer }}{{ .Name }}|{{ end }}| + +` + b := hugolib.Test(t, files) + + b.AssertFileContent("public/index.html", + "Main: Main|p1|p2||", + "Footer: Footer|p2||", + ) +} diff --git a/navigation/pagemenus.go b/navigation/pagemenus.go index 3ff49cf81..e89a366dd 100644 --- a/navigation/pagemenus.go +++ b/navigation/pagemenus.go @@ -41,20 +41,8 @@ type MenuQueryProvider interface { IsMenuCurrent(menuID string, inme *MenuEntry) bool } -func PageMenusFromPage(p Page) (PageMenus, error) { - params := p.Params() - - ms, ok := params["menus"] - if !ok { - ms, ok = params["menu"] - } - +func PageMenusFromPage(ms any, p Page) (PageMenus, error) { pm := PageMenus{} - - if !ok { - return nil, nil - } - me := MenuEntry{} SetPageValues(&me, p) diff --git a/resources/page/pagemeta/page_frontmatter.go b/resources/page/pagemeta/page_frontmatter.go index 10ba2a991..c901ab57b 100644 --- a/resources/page/pagemeta/page_frontmatter.go +++ b/resources/page/pagemeta/page_frontmatter.go @@ -101,6 +101,7 @@ type PageConfig struct { Cascade []map[string]any Sitemap config.SitemapConfig Build BuildConfig + Menus []string // User defined params. Params maps.Params |