From b8715b0cf2a4a371c45703704da9d8232f6cf907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corn=C3=A9=20Haasjes?= Date: Tue, 1 Apr 2025 19:38:31 +0200 Subject: Add support for hooks in layouts/partials/hooks Supported hooks: - head_start - head_end - body_end - footer_start --- layouts/_default/baseof.html | 2 ++ layouts/partials/footer.html | 3 +++ layouts/partials/functions/get_hook.html | 23 +++++++++++++++++++++++ layouts/partials/head.html | 5 +++++ 4 files changed, 33 insertions(+) create mode 100644 layouts/partials/functions/get_hook.html diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 7a5579d..4ff7215 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -32,6 +32,8 @@ {{ partialCached "math.html" . }} {{ end }} + {{/* Body end hook */}} + {{ partial "functions/get_hook.html" (dict "hook" "body_end" "context" .) }} diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index 579e50a..cb20f6f 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -1,5 +1,8 @@ {{ $showFooter := default true .Site.Params.showFooter }} {{ if $showFooter }} + {{/* Footer start hook */}} + {{ partial "functions/get_hook.html" (dict "hook" "footer_start" "context" .) }} + {{ if not .Site.Params.footerContent }}

Powered by Hugo diff --git a/layouts/partials/functions/get_hook.html b/layouts/partials/functions/get_hook.html new file mode 100644 index 0000000..79c9987 --- /dev/null +++ b/layouts/partials/functions/get_hook.html @@ -0,0 +1,23 @@ +{{/* + Customize layouts without overwriting files. + Hooks should be defined in the layouts/partials/hooks directory. + + Parameters: + - hook: The name of the hook to be used. + - context: The context to be passed to the partial. +*/}} + +{{ $hook := .hook }} +{{ $context := .context }} +{{ $hookName := $hook.Name }} +{{ $hookType := $hook.Type }} + +{{ if not (hasSuffix $hook ".html") }} + {{ $hook = printf "%s.html" $hook }} +{{ end }} + +{{ $hook_path := path.Join "layouts/partials/hooks" $hook }} + +{{ if fileExists $hook_path }} + {{ partial $hook_path $context }} +{{ end }} \ No newline at end of file diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 5eea693..f62f983 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -1,6 +1,9 @@ +{{/* Head start hook */}} +{{ partial "functions/get_hook.html" (dict "hook" "head_start" "context" .) }} + {{ $faviconPath := (.Site.Params.faviconPath | default "" | absURL) }} @@ -46,3 +49,5 @@ {{ end }} {{ end }} +{{/* Head end hook */}} +{{ partial "functions/get_hook.html" (dict "hook" "head_end" "context" .) }} -- cgit v1.2.3 From 160d998fa0f96219ae9b7e45d3250ea1d10edede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corn=C3=A9=20Haasjes?= Date: Tue, 1 Apr 2025 20:31:57 +0200 Subject: Fix get_hook function --- layouts/partials/functions/get_hook.html | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/layouts/partials/functions/get_hook.html b/layouts/partials/functions/get_hook.html index 79c9987..3e6b4fb 100644 --- a/layouts/partials/functions/get_hook.html +++ b/layouts/partials/functions/get_hook.html @@ -9,15 +9,11 @@ {{ $hook := .hook }} {{ $context := .context }} -{{ $hookName := $hook.Name }} -{{ $hookType := $hook.Type }} {{ if not (hasSuffix $hook ".html") }} {{ $hook = printf "%s.html" $hook }} {{ end }} -{{ $hook_path := path.Join "layouts/partials/hooks" $hook }} - -{{ if fileExists $hook_path }} - {{ partial $hook_path $context }} +{{ if fileExists (path.Join "layouts/partials/hooks" $hook) }} + {{ partial (path.Join "hooks" $hook) $context }} {{ end }} \ No newline at end of file -- cgit v1.2.3 From e8b33279b77a92e64d431904a49005348060dfd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corn=C3=A9=20Haasjes?= Date: Tue, 1 Apr 2025 21:23:08 +0200 Subject: Put body_end above footer --- layouts/_default/baseof.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 4ff7215..2decab0 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -24,6 +24,9 @@ + {{/* Body end hook */}} + {{ partial "functions/get_hook.html" (dict "hook" "body_end" "context" .) }} +

@@ -31,9 +34,6 @@ {{ if .Param "math" }} {{ partialCached "math.html" . }} {{ end }} - - {{/* Body end hook */}} - {{ partial "functions/get_hook.html" (dict "hook" "body_end" "context" .) }} -- cgit v1.2.3 From c9e49557f43a31b6072035e25f0bbfcd408a1a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corn=C3=A9=20Haasjes?= Date: Tue, 1 Apr 2025 21:25:53 +0200 Subject: Document hooks in wiki --- wiki/features/hooks.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 wiki/features/hooks.md diff --git a/wiki/features/hooks.md b/wiki/features/hooks.md new file mode 100644 index 0000000..31bbb21 --- /dev/null +++ b/wiki/features/hooks.md @@ -0,0 +1,29 @@ +--- +title: "Hooks" +date: "2024-10-6" +summary: "Layout hooks" +description: "Layout hooks" +toc: false +readTime: false +autonumber: true +math: false +showTags: false +--- + +Hooks allow to customize layouts by injecting custom code at specific points in the layout. +Hooks are defined in the `layouts/partials/hooks` directory. +The following hooks are currently available: + +- `head_start` is inserted at the beginning of the `` tag. +- `head_end` is inserted at the end of the `` tag. +- `body_end` is inserted at the end of the `` tag. +- `footer_start` is inserted at the beginning of the footer. + +To create a hook, add a file named `.html` in the `layouts/partials/hooks` directory. The file should contain the code you want to inject at that point in the layout. +For example, to preload a font, you can create a file named `head_start.html` in the `layouts/partials/hooks` directory with the following content: + +```html + +``` + +The full context is passed to the hook, so any variables available in the page context can be used in the hook. \ No newline at end of file -- cgit v1.2.3