New at recursive templating

Hi,
I’m refactoring some code I took before from a theme, but I want to make it mine with details elements instead of silly label + input + a with href, that are semantically very iffy. “Details” and summary on the other hand carry all the semantics required. Issue is I have no idea what I’m doing with loops.
Take a look:

{{ with site.GetPage "docs" }}
	{{ template "section-children" (dict "Section" . "CurrentPage" $) }}
{{ end }}

{{ define "section-children" }}
	<menu class=filetree>
	{{ range (where .Section.Pages "Params.bookHidden" false) }}
		{{ $current := eq .CurrentPage .Page }}
		{{ $draft_or_wip := or .Draft .Page.Params.wip}}
		{{ $ancestor := .Page.IsAncestor .CurrentPage }}
		<li>
		{{ if .IsSection }}
		    <details class="{{ if $ancestor }}open{{ end }} />
		    <summary class="{{ if $current }} active{{ end }}{{if $draft_or_wip}} wip{{end}}">
	        {{- partial "docs/title-menu" .Page -}}
		     </summary>
	        {{ template "section-children" (dict "Section" . "CurrentPage" $.CurrentPage) }}
		    </details>
		{{ else }}
		    <a href="{{ .Page.RelPermalink }}" class="content{{if $draft_or_wip}} wip {{end}}{{ if $current }} active{{ end }}">
		    {{- partial "docs/title-menu" .Page -}}
		    </a>
		{{ end}}
		</li>
	{{ end }}
	</menu>
{{ end }}

What could go wrong with this, to cause the following message ? I don’t understand any of this, save that some set is empty, but it used to work (with input etc) so I must have broken the logic somewhere.

ERROR render of “page” failed: “/home/drm/WEBSITE/layouts/_default/baseof.html:7:3”: execute of template failed: template: _default/single.html:7:3: executing “_default/single.html” at <partial “docs/menu” .>: error calling partial: “/home/drm/WEBSITE/layouts/partials/docs/menu.html:21:3”: execute of template failed: template: partials/docs/menu.html:21:3: executing “partials/docs/menu.html” at <partial “docs/menu-filetree” .>: error calling partial: “/home/drm/WEBSITE/layouts/partials/docs/menu-filetree.html:8:20”: execute of template failed: template: partials/docs/menu-filetree.html:8:20: executing “section-children” at <.CurrentPage>: can’t evaluate field CurrentPage in type page.Page
Built in 46 ms
Error: error building site: render: failed to render pages: render of “page” failed: “/home/drm/WEBSITE/layouts/_default/baseof.html:7:3”: execute of template failed: template: _default/single.html:7:3: executing “_default/single.html” at <partial “docs/menu” .>: error calling partial: “/home/drm/WEBSITE/layouts/partials/docs/menu.html:21:3”: execute of template failed: template: partials/docs/menu.html:21:3: executing “partials/docs/menu.html” at <partial “docs/menu-filetree” .>: error calling partial: “/home/drm/WEBSITE/layouts/partials/docs/menu-filetree.html:8:20”: execute of template failed: template: partials/docs/menu-filetree.html:8:20: executing “section-children” at <.CurrentPage>: can’t evaluate field CurrentPage in type page.Page

So my content looks like this:

/home/drm/WEBSITE/content
├──􀀂 docs
│ ├──􀇱 _index.md
│ ├──􀀂 Biology
│ │ ├──􀇱 _index.md
│ │ ├──􀇱 fallen.md
│ │ ├──􀇱 instincto.md
│ │ └──􀇱 viral_phenomenon.md
│ ├──􀀂 Love
│ │ ├──􀇱 _index.md
│ │ ├──􀇱 meta.md
│ │ └──􀇱 spirituality.md
│ └──􀀂 Politics
│ ├──􀇱 _index.md
│ ├──􀇱 ecotech.md
│ └──􀇱 politics.md
└──􀇱 intro.md

With the homepage being intro, thanks to module mounts ([[module.mounts]] source = "content/intro.md" target = "content/_index.md" ).

(corrected)

I leveled up and solved my issue:

{{ template "section-children" (dict "Section" (site.GetPage (default "docs" site.Params.BookSection)) "CurrentPage" $) }}

{{ define "section-children" }}
 <menu class=filetree>
{{ range $context1 := (where .Section.Pages "Params.bookHidden" false) }}
	<li>
	{{ with (dict "Page" . "CurrentPage" $.CurrentPage) }}
		{{ $current := eq .CurrentPage .Page }}
		{{ $draft := or .Params.wip .Draft }}
		{{ $ancestor := .Page.IsAncestor .CurrentPage }}
		{{ if .Page.IsSection }}
		 <details {{ if $ancestor }}class=ancestor open{{ end }}>
	    <summary {{if $draft}}class=wip{{end}}>
	    {{- partial "docs/title-menu" .Page -}}
	    </summary>
	    {{ template "section-children" (dict "Section" $context1 "CurrentPage" $.CurrentPage) }}
	</details>
		 {{ else }}
	    <a href="{{ .Page.RelPermalink }}" class="{{if or .Page.Draft .Page.Params.wip}}wip {{end}}content {{ if $current }}active{{ end }}">   {{- partial "docs/title-menu" .Page -}} </a>
		  {{ end }}
	{{end}}
	</li>
{{ end }}
</menu>
{{ end }}

Now I’m proud.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.