Skip to content

Commit

Permalink
feat: more flexible hooks system
Browse files Browse the repository at this point in the history
1. Use more standard hook names:
- head-start
- head-end
- body-end
- footer-start
- toc-start
- toc-end

2. Enable multiple files per hook. For example, a starter template may have `hooks/head-end/tpl.html` and a user may have customizations in `hooks/head-end/user.html`

custom_head.html -> hooks/head-end/custom.html
custom_js.html -> hooks/body-end/custom.html

Deprecates custom_head.html and custom_js.html but they remain supported for now.
  • Loading branch information
gcushen committed Jun 2, 2022
1 parent d266249 commit acd7495
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 209 deletions.
4 changes: 2 additions & 2 deletions wowchemy/layouts/partials/book_layout.html
Expand Up @@ -12,15 +12,15 @@
{{/* Show ToC by default. */}}
{{ if ne .Params.toc false }}
<div class="d-none d-xl-block col-xl-2 docs-toc">
{{ partial "hooks/page_toc_start" . }}
{{ partial "functions/get_hook" (dict "hook" "toc-start" "context" .) }}

<ul class="nav toc-top">
<li><a href="#" id="back_to_top" class="docs-toc-title">{{ i18n "on_this_page" }}</a></li>
</ul>

{{ .TableOfContents }}

{{ partial "hooks/page_toc_end" . }}
{{ partial "functions/get_hook" (dict "hook" "toc-end" "context" .) }}
</div>
{{ end }}

Expand Down
4 changes: 0 additions & 4 deletions wowchemy/layouts/partials/custom_head.html

This file was deleted.

4 changes: 0 additions & 4 deletions wowchemy/layouts/partials/custom_js.html

This file was deleted.

18 changes: 18 additions & 0 deletions wowchemy/layouts/partials/functions/get_hook.html
@@ -0,0 +1,18 @@
{{/* Function to inject custom code into layouts without overriding files. */}}
{{/* Input: hook folder name (str) */}}
{{/* Output: loaded (bool) */}}

{{ $loaded := false }}
{{ $partial_dir := printf "hooks/%s/" .hook }}
{{ $hook_dir_path := path.Join "layouts/partials" $partial_dir }}
{{ if fileExists $hook_dir_path }}
{{ range os.ReadDir $hook_dir_path }}
{{ if not .IsDir }}
{{ $partial_path := path.Join $partial_dir .Name }}
{{ partial $partial_path . }}
{{ $loaded = true }}
{{ end }}
{{ end }}
{{ end }}
{{/* The return statement below is for debug purposes only and prevents the above partial(s) being included in the page */}}
{{/* return $loaded */}}
2 changes: 0 additions & 2 deletions wowchemy/layouts/partials/hooks/footer.html

This file was deleted.

Empty file.
Empty file.
2 changes: 1 addition & 1 deletion wowchemy/layouts/partials/site_footer.html
@@ -1,6 +1,6 @@
<footer class="site-footer">

{{ partial "hooks/footer" . }}
{{ partial "functions/get_hook" (dict "hook" "footer-start" "context" .) }}

{{ if .IsTranslated | and site.Params.footer.show_translations }}
<div class="powered-by d-flex flex-wrap pb-2 justify-content-center">
Expand Down
11 changes: 10 additions & 1 deletion wowchemy/layouts/partials/site_head.html
Expand Up @@ -9,6 +9,9 @@
<meta name="generator" content="Wowchemy {{ site.Data.wowchemy.version }} for Hugo" />
{{ end }}

{{/* EXTENSIBILITY HOOK: HEAD-START */}}
{{ partial "functions/get_hook" (dict "hook" "head-start" "context" .) }}

{{ if .Params.private }}
<meta name="robots" content="noindex" />
{{- end -}}
Expand Down Expand Up @@ -305,7 +308,13 @@

{{ partial "jsonld/main" (dict "page" . "summary" $desc) }}
{{ partial "cookie_consent" . }}
{{ partial "custom_head" . }}

<title>{{$title}}</title>

{{/* EXTENSIBILITY HOOK: HEAD-END */}}
{{/* Deprecated custom_head hook */}}
{{ if templates.Exists "partials/custom_head" }}
{{ partial "custom_head" . }}
{{ end }}
{{ partial "functions/get_hook" (dict "hook" "head-end" "context" .) }}
</head>
395 changes: 200 additions & 195 deletions wowchemy/layouts/partials/site_js.html

Large diffs are not rendered by default.

0 comments on commit acd7495

Please sign in to comment.