FWIW, I assumed that I could cut up my baseof.html
template into constituent files to organise them in a more granular wayβ¦ Perhaps more granularly than many users tend to do. Hereβs the structrure of my layouts/
folder:
layouts/
βββ _default/
β βββ baseof.html
β βββ list.html
β βββ single.html
βββ partials/
β βββ _global/
β β βββ 0-head/
β β β βββ primary_head.html
β β βββ 1-body/
β β β βββ primary_body.html
β β βββ 2-header/
β β β βββ primary_header.html
β β βββ 3-main/
β β β βββ primary_main.html
β β βββ 4-footer/
β β βββ primary_footer.html
β βββ menus/
β βββ basic/
β β βββ recursion-full.html
β β βββ recursion-none.html
β β βββ sectional.html
β βββ iconographic/
β β βββ social-media.html
β βββ special-purpose/
β βββ a11y-jump-navigation.html
βββ shortcodes/
βββ 404.html
Several of the partials, in turn, reference partials that are βmore deeply nestedβ.
To explain, here is the code in two of the files above:
layouts/_default/baseof.html
<!DOCTYPE html>
<html lang="en" class="w-full h-full">
{{- partial "_global/0-head/primary_head" . }}
{{- partial "_global/1-body/primary_body" . -}}
{{- partial "debug/json.html" (dict "siteParams" .Site.Params "currentContext" site.Menus) -}}
</html>
layouts/partials/_global/1-body/primary_body.html
<body class="w-full h-full">
<div class="w-full flex flex-col md:flex-row md:flex-wrap min-h-screen">
{{- partial "menus/special-purpose/a11y-jump-navigation" . }}
{{- partial "_global/2-header/primary_header" . }}
{{- partial "_global/3-main/primary_main" . }}
{{- partial "_global/4-footer/primary_footer" . }}
</div>
</body>
layouts/partials/_global/3-main/primary_main.html
{{- block "body-global-main" . }}
<main id="main" role="main" class="body-global-main">
{{ .Content }}
</main>
{{ end -}}
What Iβd been hoping to do was then define {{block "..."}}
blocks in the various partials and then βredefineβ ({{define "..."}}
) them in page templates of greater specificity, only overriding/redefining blocks
that were in need of βSectionβ specific changes.
I found this Discourse thread because, after having setup my code as displayed above, I began working on overriding the body-global-main
block (initialised in primary_main.html
as illustrated above) in layouts/_default/home.html
β¦ and it wasnβt working.
I can see now from @bep 's comment in this thread that the constructs {{block}}
and {{define}}
are βonly meant to be used in baseof.html
templatesβ. For anyone who was thinking of doing something like what I was trying, hereβs the page to look up βbaseofβ (and other such) templates: Template lookup order .
It really would be great if we could cut up our baseof.html
templates so that they are easier to parse through but, lacking that capability, for others who come across this thread, the way to go as of today is to:
- Keep ALL markup that you want to override using
{{block}}/{{define}}
constructs in a single fileβ¦
- Then, simply accept that weβll be duplicating the
baseof.html
at least a few (e.g., copy/paste in home.html
which is what I need to do in my case) and then override in higher-specificity List/Single pages as needed.
The above doesnβt preclude a mix of cut-up of code like <head>
and opening/closing <body> </body>
tags into partial files. This should at least help keep things a bit cleaner. E.g. - snippets of what Iβm doing now:
layouts/_default/baseof.html
<!DOCTYPE html>
<html lang="en" class="w-full h-full">
{{- partial "_global/0-head/baseof_head" . }}
layouts/_default/baseof.html
<!DOCTYPE html>
<html lang="en" class="w-full h-full">
{{- partial "_global/0-head/home_head" . }}
The above illustrates that Iβve moved removed the use of block
({{block "head-styles-global" . }}
) in those two partials and thus, Iβm able to better control and override which JS and CSS files are used globally (in {{partial "_global/0-head/baseof_head" . }}
) by overriding the partial that is loaded in layouts/_default/home.html
to be {{- partial "_global/0-head/home_head" . }}
instead. To explain, the siteβs home page will have vastly different styling and JS requirements and so, they shouldnβt be loaded with every page.
I do hope that maybe we can simply override {{block}}
with {{define}}
across βsecondaryβ baseof templates in the future. I sorta wonder if maybe Hugo could provide this capability by defining hierarchical relationships between types of Template files like so:
- ROOT (any block defined here can be overridden in children): baseof.html (and variations thereof)
- Second-Tier: home, list, single, section, page (
{{define}}
blocks in these could then override {{block}}
definitions in baseof.html
. Templates of this tier can also define new blocks of their own.
- Third-Tier:
content/TREE-STRUCTURE
based specificity templates. E.g. content/pages
and content/posts
would both imply that they may have customised layouts in layouts/pages/[list|single].html
and layouts/posts/[list|single].html
. Third-Tier templates wouldnβt be able to define new {{block}}
blocks, theyβd only be able to override blocks defined in βlower levelβ tier templates.
I hope this info helps others in the future!
Related threads:
- Blocks and define override in hugo theme
- Support Base Templates and Blocks in partial rendering