Skip to content

fix : Hugo partial rendering error #16475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions layouts/partials/navigation_level.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{{ end }}
{{ end }}

<ul role="{{ if $top }}tree{{ else }}group{{ end }}" aria-expanded="{{ if $collapse }}false{{ else }}true{{ end }}"{{ if $leafSection }} class="leaf-section"{{ end }} {{ if $labelledby}}aria-labelledby="{{ $labelledby }}"{{ end }}>
<ul role="{{ if $top }}tree{{ else }}group{{ end }}" aria-expanded="{{ if $collapse }}false{{ else }}true{{ end }}"{{ if $leafSection }} class="leaf-section"{{ end }} {{ if $labelledby }}aria-labelledby="{{ $labelledby }}"{{ end }}>
{{ range $pages }}
{{ $pageLocation := (path.Dir (path.Dir .File.Dir)) }}
{{ if and (eq $parentDir $pageLocation) (not .Params.draft) }}
Expand All @@ -42,22 +42,28 @@
{{ end }}

{{ if not .IsPage }}
<li role="treeitem" aria-label="{{ $linktitle}}">
<li role="treeitem" aria-label="{{ $linktitle }}">
{{ $collapse := not (.IsAncestor $current) }}
{{ if eq . $current }}
{{ $collapse = true }}
{{ end }}

{{if lt (len $pageLocation) 6 -}}
<a class='main' title="{{ $desc }}" href="{{ .Permalink }}">{{ $linktitle}}</a>
{{ partial "navigation_level.html" (dict "pages" $pages "parent" . "current" $current "collapse" false "top" false "labelledby" "" ) .CurrentSection }}
{{ else if (.CurrentSection.IsAncestor $current) }}
<button {{ if not $collapse }} class="show" {{ end }} aria-hidden="true" tabindex="-1" ></button><a title="{{ $desc }}" href="{{ .Permalink }}">{{ $linktitle}}</a>
{{ partial "navigation_level.html" (dict "pages" $pages "parent" . "current" $current "collapse" $collapse "top" false "labelledby" "" ) }}
{{- else -}}
<button {{ if not $collapse }} class="show" {{ end }} aria-hidden="true" tabindex="-1" ></button><a title="{{ $desc }}" href="{{ .Permalink }}">{{ $linktitle}}</a>
{{ partialCached "navigation_level.html" (dict "pages" $pages "parent" . "current" $current "collapse" $collapse "top" false "labelledby" "" ) .CurrentSection }}
{{- end -}}
{{ if lt (len $pageLocation) 6 }}
<a class='main' title="{{ $desc }}" href="{{ .Permalink }}">{{ $linktitle }}</a>
{{ if and .CurrentSection (ne .CurrentSection .) (ne .CurrentSection $parent) (ne .CurrentSection $current) .CurrentSection.Pages }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you summarise the logic for me please?

basically, for each partial, I'm seeing "if all these things are true" - not root, not parent, not current etc. Why both .CurrentSection and .CurrentSection.Pages?

Copy link
Author

@Ajay-singh1 Ajay-singh1 May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I will break down the logic for you btw I don't know Hugo templating that much and just started learning.

What it does:-

1.CurrentSection:- Ensures we're dealing with something that belongs to a section (not top-level).

  1. (ne .CurrentSection .):- Prevents self-recursion

3.(ne .CurrentSection $parent):- Don’t recurse back into the section that already called this one.

4.(ne .CurrentSection $current):- Prevents duplicating the current page’s section again.

5.CurrentSection.Pages:- Only recurse if there are actual child pages/sections to display

{{ partial "navigation_level.html" (dict "pages" .CurrentSection.Pages "parent" . "current" $current "collapse" false "top" false "labelledby" "") }}
{{ end }}
{{ else if .CurrentSection.IsAncestor $current }}
<button {{ if not $collapse }} class="show" {{ end }} aria-hidden="true" tabindex="-1"></button><a title="{{ $desc }}" href="{{ .Permalink }}">{{ $linktitle }}</a>
{{ if and .CurrentSection (ne .CurrentSection .) (ne .CurrentSection $parent) (ne .CurrentSection $current) .CurrentSection.Pages }}
{{ partial "navigation_level.html" (dict "pages" .CurrentSection.Pages "parent" . "current" $current "collapse" $collapse "top" false "labelledby" "") }}
{{ end }}
{{ else }}
<button {{ if not $collapse }} class="show" {{ end }} aria-hidden="true" tabindex="-1"></button><a title="{{ $desc }}" href="{{ .Permalink }}">{{ $linktitle }}</a>
{{ if and .CurrentSection (ne .CurrentSection .) (ne .CurrentSection $parent) (ne .CurrentSection $current) .CurrentSection.Pages }}
{{ partial "navigation_level.html" (dict "pages" .CurrentSection.Pages "parent" . "current" $current "collapse" $collapse "top" false "labelledby" "") }}
{{ end }}
{{ end }}
</li>
{{ else }}
<li role="none">
Expand Down