fix(layouts): replace deprecated .Site.BuildDrafts with HUGO_PARAMS_MENU_DRAFT env var

Use getenv "HUGO_PARAMS_MENU_DRAFT" to control draft menu item visibility,
replacing the deprecated .Site.BuildDrafts (Hugo v0.156.0). (#702)

Also refactors menu range loops to use `continue` for reduced nesting.
This commit is contained in:
Cell
2026-08-19 12:29:36 +08:00
parent 224621b114
commit c65852d882
3 changed files with 106 additions and 95 deletions
+2 -1
View File
@@ -99,7 +99,8 @@ weight = 1
[menus.main.params]
# Add CSS class to a specific menu item.
class = ""
# Whether set as a draft menu item whose function is similar to a draft post/page.
# Whether set as a draft menu item, hidden by default.
# Visible when env var is set: HUGO_PARAMS_MENU_DRAFT=true hugo server
draft = false
# Add FontAwesome icon to a specific menu item.
icon = "fa-solid fa-archive"
+19 -9
View File
@@ -10,7 +10,13 @@
Called from: layouts/baseof.html.
*/ -}}
{{- $buildDrafts := .Page.Draft -}}
{{- /*
Whether to show draft menu items in the navigation.
Controlled via env var at build time: HUGO_PARAMS_MENU_DRAFT=true hugo server
Replaces the deprecated .Site.BuildDrafts (Hugo v0.156.0).
See: https://discourse.gohugo.io/t/deprecations-in-v0-156-0/56732/11
*/ -}}
{{- $menuDraft := getenv "HUGO_PARAMS_MENU_DRAFT" -}}
{{- $relLangURL := relLangURL "" -}}
{{- /* Desktop header */ -}}
@@ -57,7 +63,9 @@
{{- with .Page -}}
{{- $url = .RelPermalink -}}
{{- end -}}
{{- if (ne .Params.Draft true) | or $buildDrafts | and (ne .Params.Type "mobile") -}}
{{- if (eq .Params.Type "mobile") | or (and (eq .Params.Draft true) (ne $menuDraft "true")) -}}
{{ continue }}
{{- end -}}
<li class="menu-item{{ if $.IsMenuCurrent `main` . | or ($.HasMenuCurrent `main` .) | or (eq $.RelPermalink $url) }} active{{ end }}{{ with .Params.Class }} {{ . }}{{ end }}{{ if .HasChildren }} has-children{{ end }}">
<a class="menu-link"
{{- with $url }} href="{{ $url }}"{{ else }} href="javascript:void(0);"{{ end }}
@@ -80,7 +88,9 @@
{{- with $v.Page -}}
{{- $url = .RelPermalink -}}
{{- end -}}
{{- if (ne $v.Params.Draft true) | or $buildDrafts | and (ne $v.Params.Type "mobile") -}}
{{- if (eq $v.Params.Type "mobile") | or (and (eq $v.Params.Draft true) (ne $menuDraft "true")) -}}
{{ continue }}
{{- end -}}
{{- if $k | and $v.Params.Divided -}}
<li class="menu-item-divider" aria-hidden="true"></li>
{{- end -}}
@@ -97,12 +107,10 @@
</a>
</li>
{{- end -}}
{{- end -}}
</ul>
{{- end -}}
</li>
{{- end -}}
{{- end -}}
{{- if .Site.Menus.main -}}
<li class="menu-item delimiter"></li>
{{- end -}}
@@ -220,7 +228,9 @@
{{- with .Page -}}
{{- $url = .RelPermalink -}}
{{- end -}}
{{- if (ne .Params.Draft true) | or $buildDrafts | and (ne .Params.Type "desktop") -}}
{{- if (eq .Params.Type "desktop") | or (and (eq .Params.Draft true) (ne $menuDraft "true")) -}}
{{ continue }}
{{- end -}}
<li class="menu-item{{ if $.IsMenuCurrent `main` . | or ($.HasMenuCurrent `main` .) | or (eq $.RelPermalink $url) }} active{{ end }}{{ with .Params.Class }} {{ . }}{{ end }}">
{{- if .HasChildren -}}
<span class="nested-item">
@@ -245,7 +255,9 @@
{{- with $v.Page -}}
{{- $url = .RelPermalink -}}
{{- end -}}
{{- if (ne $v.Params.Draft true) | or $buildDrafts | and (ne $v.Params.Type "desktop") -}}
{{- if (eq $v.Params.Type "desktop") | or (and (eq $v.Params.Draft true) (ne $menuDraft "true")) -}}
{{ continue }}
{{- end -}}
{{- if $k | and $v.Params.Divided -}}
<li class="menu-item menu-item-divider" aria-hidden="true"></li>
{{- end -}}
@@ -262,7 +274,6 @@
</a>
</li>
{{- end -}}
{{- end -}}
</ul>
{{- else -}}
<a class="menu-link"
@@ -278,7 +289,6 @@
{{- end -}}
</li>
{{- end -}}
{{- end -}}
<li class="menu-item menu-system">
{{- if hugo.IsMultilingual -}}
<span class="menu-system-item language-switch">
+1 -1
View File
@@ -8,7 +8,7 @@
Called from: layouts/baseof.html.
*/ -}}
{{- hugo.Store.Set "version" "v1.0.0-alpha-mszfs8xu" -}}
{{- hugo.Store.Set "version" "v1.0.0-alpha-mszlegf7" -}}
{{- .Store.Set "this" dict -}}
{{- partial "init/detection-env.html" . -}}