From ed9e769b2dc156c10231060c0a62847ef2dec91b Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Wed, 4 Jun 2025 17:54:51 +0200 Subject: [PATCH] use partial for footer scripts --- layouts/_default/single.html | 40 +--------------- layouts/blog/single.html | 41 +---------------- layouts/partials/scripts/charts.html | 30 ++++++++++++ .../scripts/show_post_locations_on_map.html | 46 +++++++++++++++++++ layouts/partials/single/footer-scripts.html | 46 +++++++++++++++++++ 5 files changed, 125 insertions(+), 78 deletions(-) create mode 100644 layouts/partials/scripts/charts.html create mode 100644 layouts/partials/scripts/show_post_locations_on_map.html create mode 100644 layouts/partials/single/footer-scripts.html diff --git a/layouts/_default/single.html b/layouts/_default/single.html index ef602ca..da6a9f9 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -32,45 +32,7 @@ {{ end }} {{ define "footer-scripts" }} - {{ with .Params.locations }} - - {{ end }} - {{ with .Params.scripts }} - {{ if in . "charts" }} - {{ $chartsConfigJS := $.Page.Resources.GetMatch "charts-init" | fingerprint }} - - - - - {{ end }} - {{ end }} + {{- partialCached "single/footer-scripts.html" ( dict "pc" . "sc" $ ) . }} {{ end }} {{ define "main-header" }} diff --git a/layouts/blog/single.html b/layouts/blog/single.html index 81aec94..08262db 100644 --- a/layouts/blog/single.html +++ b/layouts/blog/single.html @@ -30,48 +30,11 @@ {{ end }} {{ define "footer-scripts" }} - {{ with .Params.locations }} - - {{ end }} - {{ if $.HasShortcode "chart" }} - {{ $chartsConfigJS := $.Page.Resources.GetMatch "charts-init" | fingerprint }} - - - - - {{ end }} + {{- partialCached "single/footer-scripts.html" ( dict "pc" . "sc" $ ) . -}} {{ end }} {{ define "main-header" }} - {{ partial "single/main-header.html" ( dict "pc" . "sc" $ ) }} + {{- partial "single/main-header.html" ( dict "pc" . "sc" $ ) -}} {{ end }} {{ define "main-body" }} diff --git a/layouts/partials/scripts/charts.html b/layouts/partials/scripts/charts.html new file mode 100644 index 0000000..1195c00 --- /dev/null +++ b/layouts/partials/scripts/charts.html @@ -0,0 +1,30 @@ +{{- /* + Renders charts on a post. + + @params . Chart JS config script. + */ +-}} + + + +{{- if ( not . ) -}} + {{- errorf "No config JS defined" -}} +{{- end -}} + + + +{{- $chartsConfigJS := . -}} + + + + + + + diff --git a/layouts/partials/scripts/show_post_locations_on_map.html b/layouts/partials/scripts/show_post_locations_on_map.html new file mode 100644 index 0000000..065871e --- /dev/null +++ b/layouts/partials/scripts/show_post_locations_on_map.html @@ -0,0 +1,46 @@ +{{- /* + Renders a post's locations on a map. + + @params pc Page context. + @params locations_data Site data of locations. + @params locations Post locations. + */ +-}} + + + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context passed" -}} +{{- end -}} + +{{- if ( not ( isset . "locations_data" ) ) -}} + {{- errorf "No location site data defined" -}} +{{- end -}} + +{{- if ( not ( isset . "locations" ) ) -}} + {{- errorf "No post locations defined" -}} +{{- end -}} + + + + diff --git a/layouts/partials/single/footer-scripts.html b/layouts/partials/single/footer-scripts.html new file mode 100644 index 0000000..f46d86c --- /dev/null +++ b/layouts/partials/single/footer-scripts.html @@ -0,0 +1,46 @@ +{{- /* + Renders a single item's required footer JS scripts. + + @params sc Site context. + @params pc Page context. + */ +-}} + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context passed" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context passed" -}} +{{- end -}} + +{{- if ( and ( .pc.HasShortcode "chart" ) ( not ( .pc.Page.Resources.GetMatch "charts-init" ) ) ) -}} + {{- errorf "No 'chart-init' page resource defined (%q)" .pc.Page.File.Path -}} +{{- end -}} + + + +{{ if .pc.Params.locations }} + {{- + partialCached + "scripts/show_post_locations_on_map.html" + ( dict + "pc" .pc + "locations_data" .sc.Site.Data.locations + "locations" .pc.Params.locations + ) + .pc.Params.locations + -}} +{{ end }} + +{{ if .pc.HasShortcode "chart" }} + {{- $configJs := ( .pc.Page.Resources.GetMatch "charts-init" | fingerprint ) -}} + {{- + partialCached + "scripts/charts.html" + $configJs + $configJs + -}} +{{ end }}