Merge pull request #112 from crnh/crnh/feature/hooks

This commit is contained in:
Francesco Tomaselli 2025-04-02 12:19:08 +02:00 committed by GitHub
commit 5ff1dc7c3b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 1 deletions

View file

@ -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>

View file

@ -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>

View file

@ -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 }}

View file

@ -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" .) }}

29
wiki/features/hooks.md Normal file
View file

@ -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.