summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancesco Tomaselli <tomaselli.fr@gmail.com>2025-04-02 12:19:08 +0200
committerGitHub <noreply@github.com>2025-04-02 12:19:08 +0200
commitb8f85832c51ba1ab21700e7b7a674f563b7a2657 (patch)
tree5321c0152756c3a13213b03a28cb08427455992c
parent9794243a3ff85aae024d6a8f7439702cc66f3f73 (diff)
parentc9e49557f43a31b6072035e25f0bbfcd408a1a87 (diff)
Merge pull request #112 from crnh/crnh/feature/hooks
-rw-r--r--layouts/_default/baseof.html4
-rw-r--r--layouts/partials/footer.html3
-rw-r--r--layouts/partials/functions/get_hook.html19
-rw-r--r--layouts/partials/head.html5
-rw-r--r--wiki/features/hooks.md29
5 files changed, 59 insertions, 1 deletions
diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html
index 7a5579d..2decab0 100644
--- a/layouts/_default/baseof.html
+++ b/layouts/_default/baseof.html
@@ -24,6 +24,9 @@
</main>
</div>
+ {{/* Body end hook */}}
+ {{ partial "functions/get_hook.html" (dict "hook" "body_end" "context" .) }}
+
<footer>
{{ partial "footer.html" . }}
</footer>
@@ -31,7 +34,6 @@
{{ if .Param "math" }}
{{ partialCached "math.html" . }}
{{ end }}
-
</body>
<script src="{{ "js/theme-switch.js" | relURL }}"></script>
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 }}
<p>Powered by
<a href="https://gohugo.io/">Hugo</a>
diff --git a/layouts/partials/functions/get_hook.html b/layouts/partials/functions/get_hook.html
new file mode 100644
index 0000000..3e6b4fb
--- /dev/null
+++ b/layouts/partials/functions/get_hook.html
@@ -0,0 +1,19 @@
+{{/*
+ 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 }}
+
+{{ if not (hasSuffix $hook ".html") }}
+ {{ $hook = printf "%s.html" $hook }}
+{{ end }}
+
+{{ if fileExists (path.Join "layouts/partials/hooks" $hook) }}
+ {{ partial (path.Join "hooks" $hook) $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 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
+{{/* Head start hook */}}
+{{ partial "functions/get_hook.html" (dict "hook" "head_start" "context" .) }}
+
{{ $faviconPath := (.Site.Params.faviconPath | default "" | absURL) }}
<link rel="icon" type="image/ico" href="{{ $faviconPath }}/favicon.ico">
@@ -46,3 +49,5 @@
{{ end }}
{{ end }}
+{{/* Head end hook */}}
+{{ partial "functions/get_hook.html" (dict "hook" "head_end" "context" .) }}
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 `<head>` tag.
+- `head_end` is inserted at the end of the `<head>` tag.
+- `body_end` is inserted at the end of the `<body>` tag.
+- `footer_start` is inserted at the beginning of the footer.
+
+To create a hook, add a file named `<hook_name>.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
+<link rel="preload" href="/fonts/Literata/Literata-Light.woff2" type="font/woff2" as="font" crossorigin>
+```
+
+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