From 414ea49799f36226333691d8666b3518a9a8c48c Mon Sep 17 00:00:00 2001 From: Karl Date: Sun, 12 Sep 2021 22:02:00 -0500 Subject: Implement twitter and og change with new template Refactor out the albumthumb selection so we can reuse it to select images for opengraph and twitter_cards, then modify the internal opengraph and twitter_card templates so they reflect the same design decisions in the head template and so they use the right image (some code is duplicative still but...) --- layouts/partials/head.html | 17 ++++++----------- layouts/partials/opengraph_twittercard.html | 19 +++++++++++++++++++ layouts/partials/scratch_set_retalbumthumb.html | 24 ++++++++++++++++++++++++ layouts/partials/sect_and_img_content.html | 18 +++--------------- 4 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 layouts/partials/opengraph_twittercard.html create mode 100644 layouts/partials/scratch_set_retalbumthumb.html diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 0a3cdc7..882cd91 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -9,17 +9,12 @@ -{{- template "_internal/opengraph.html" . }} -{{- template "_internal/twitter_cards.html" . }} -{{- if not .Params.images }} - {{- with .Params.albumthumb }} - {{- /* Manually add preview from albumthumb if internal tmplate misses it */}} - {{- $image := resources.Get . }} - - - - {{- end }} -{{- end }} +{{- partial "scratch_set_retalbumthumb.html" . }} +{{- /* Note that the Scratch below must come from . context, not $ */}} +{{- .Scratch.Set "image" (.Scratch.Get "retalbumthumb") }} + +{{- partial "opengraph_twittercard.html" . }} + {{ with .Site.Params.googlesiteverification }}{{ end }} diff --git a/layouts/partials/opengraph_twittercard.html b/layouts/partials/opengraph_twittercard.html new file mode 100644 index 0000000..e401f21 --- /dev/null +++ b/layouts/partials/opengraph_twittercard.html @@ -0,0 +1,19 @@ +{{- $description := $.Param "description" }} + + + + +{{- with $description }}{{ end }} +{{- with .Site.Social.twitter }}{{- end }} + + + +{{- with $description }}{{ end }} + + + +{{- with .Params.locale }}{{ end }} +{{- with .Site.Title }}{{ end }} + +{{- /* Facebook Page Admin ID for Domain Insights */}} +{{- with .Site.Social.facebook_admin }}{{ end }} diff --git a/layouts/partials/scratch_set_retalbumthumb.html b/layouts/partials/scratch_set_retalbumthumb.html new file mode 100644 index 0000000..c718581 --- /dev/null +++ b/layouts/partials/scratch_set_retalbumthumb.html @@ -0,0 +1,24 @@ +{{- /* + Sets retalbumthumb in .Scratch to the correct albumthumb. + PLEASE NOTE: partials can only get local "dot" . context, they + do not get the global $ context. Therefore, you must get the returned + scratch data from the . context, not from $. + + Requires . to be set to the right section. + */}} + +{{- .Scratch.Delete "retalbumthumb" }} + +{{- /* If no albumthumb is set, get the first image*/}} +{{- if or (not (isset .Params "albumthumb")) (eq .Params.albumthumb "") }} + {{- $imgglob := printf "%s" (path.Join .File.Dir "**") }} + {{- $imageresources := where (resources.Match $imgglob) "ResourceType" "image" }} + {{- .Scratch.Set "retalbumthumb" (index $imageresources 0) }} +{{- end }} + +{{- /* Otherwise get the albumthumb*/}} +{{- with .Params.albumthumb }} + {{- $image := resources.Get . }} + {{- /* The $ here with Scratch means the context outside the with */}} + {{- $.Scratch.Set "retalbumthumb" $image }} +{{- end }} diff --git a/layouts/partials/sect_and_img_content.html b/layouts/partials/sect_and_img_content.html index 52d385a..4583a3d 100644 --- a/layouts/partials/sect_and_img_content.html +++ b/layouts/partials/sect_and_img_content.html @@ -19,22 +19,10 @@ {{- $title := .Title }} {{- $link := .RelPermalink }} - {{- $.Scratch.Delete "image" }} + {{- partial "scratch_set_retalbumthumb.html" . }} + {{- /* Note that the Scratch below must come from . context, not $ */}} + {{- $image := .Scratch.Get "retalbumthumb" }} - {{- /* If no albumthumb is set, get the first image*/}} - {{- if or (not (isset .Params "albumthumb")) (eq .Params.albumthumb "") }} - {{- $imgglob := printf "%s" (path.Join .File.Dir "**") }} - {{- $imageresources := where (resources.Match $imgglob) "ResourceType" "image" }} - {{- $.Scratch.Set "image" (index $imageresources 0) }} - {{- end }} - - {{- /* Otherwise get the albumthumb*/}} - {{- with .Params.albumthumb }} - {{- $image := resources.Get . }} - {{- $.Scratch.Set "image" $image }} - {{- end }} - - {{- $image := $.Scratch.Get "image" }} {{- if not $image }} {{- errorf (printf "When processing '%s', no thumbnail image found for: %s" $.Page.File.Path $title) }} {{- else }} -- cgit v1.2.3