From 37a749ce7981b2ae45ec2e164349a5ebcf41ea8d Mon Sep 17 00:00:00 2001 From: Alexander Zhang Date: Fri, 17 Jun 2022 15:39:18 -0700 Subject: Simplify template logic Some of the logic in the templates can be simplified by replacing `if` actions with `with` actions and Hugo functions. This reduces code repetition. --- layouts/_default/baseof.html | 14 ++++++------ layouts/partials/google-analytics.html | 27 +++++------------------ layouts/partials/google-tag-manager-noscript.html | 13 +++-------- layouts/partials/google-tag-manager.html | 13 +++-------- layouts/partials/sub-footer.html | 6 ++--- 5 files changed, 21 insertions(+), 52 deletions(-) diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 90b2e77..1b38f2e 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -4,7 +4,7 @@ - {{ block "title" . }}{{ if .Params.meta_title }}{{ .Params.meta_title }}{{ else }}{{ .Title }} - {{ .Site.Title }}{{ end }}{{ end }} + {{ block "title" . }}{{ .Params.meta_title | default (printf "%s - %s" .Title .Site.Title) }}{{ end }} @@ -22,15 +22,15 @@ {{ block "header_css" . }}{{ end }} {{ block "meta_tags" . }} - {{ if .Params.description }}{{ end }} - {{ if .Params.meta_title }}{{ else }}{{ end }} + {{ with .Params.description }}{{ end }} + - {{ if .Params.image }}{{ else if .Site.Params.seo.meta_og_image }}{{ end }} - {{ if .Params.description }}{{ end }} + {{ with .Params.image | default .Site.Params.seo.meta_og_image }}{{ end }} + {{ with .Params.description }}{{ end }} - {{ if .Site.Params.seo.meta_twitter_site }}{{ end }} - {{ if .Site.Params.seo.meta_twitter_creator }}{{ end }} + {{ with .Site.Params.seo.meta_twitter_site }}{{ end }} + {{ with .Site.Params.seo.meta_twitter_creator }}{{ end }} {{ end }} diff --git a/layouts/partials/google-analytics.html b/layouts/partials/google-analytics.html index 296d4af..cf29915 100644 --- a/layouts/partials/google-analytics.html +++ b/layouts/partials/google-analytics.html @@ -1,10 +1,7 @@ -{{- if .Site.IsServer -}} - -{{ else }} -{{ $gid := (getenv "HUGO_GOOGLE_ANALYTICS_ID") }} -{{ if $gid }} +{{- if not .Site.IsServer -}} +{{ with getenv "HUGO_GOOGLE_ANALYTICS_ID" | default .Site.Params.google_analytics_id }} - + -{{ else }} -{{ if .Site.Params.google_analytics_id }} - - - {{ end }} -{{ end}} -{{ end }} \ No newline at end of file +{{ end }} diff --git a/layouts/partials/google-tag-manager-noscript.html b/layouts/partials/google-tag-manager-noscript.html index 0292e6e..cf75eb9 100644 --- a/layouts/partials/google-tag-manager-noscript.html +++ b/layouts/partials/google-tag-manager-noscript.html @@ -1,12 +1,5 @@ -{{- if .Site.IsServer -}} - -{{ else }} -{{ $gid := (getenv "HUGO_GTM_ID") }} -{{ if $gid }} - -{{ else }} -{{ if .Site.Params.google_tag_manager_id }} - -{{ end }} +{{- if not .Site.IsServer -}} +{{ with getenv "HUGO_GTM_ID" | default .Site.Params.google_tag_manager_id }} + {{ end }} {{ end }} diff --git a/layouts/partials/google-tag-manager.html b/layouts/partials/google-tag-manager.html index 950eac8..a72c490 100644 --- a/layouts/partials/google-tag-manager.html +++ b/layouts/partials/google-tag-manager.html @@ -1,12 +1,5 @@ -{{- if .Site.IsServer -}} - -{{ else }} -{{ $gid := (getenv "HUGO_GTM_ID") }} -{{ if $gid }} - -{{ else }} -{{ if .Site.Params.google_tag_manager_id }} - -{{ end }} +{{- if not .Site.IsServer -}} +{{ with getenv "HUGO_GTM_ID" | default .Site.Params.google_tag_manager_id }} + {{ end }} {{ end }} diff --git a/layouts/partials/sub-footer.html b/layouts/partials/sub-footer.html index 297b782..3e56837 100644 --- a/layouts/partials/sub-footer.html +++ b/layouts/partials/sub-footer.html @@ -6,11 +6,11 @@ {{ if .Site.Data.social }} {{ partial "social.html" . }} {{ end }} - {{ if .Site.Params.footer.copyright_text }} - + {{ with .Site.Params.footer.copyright_text }} + {{ end }} - \ No newline at end of file + -- cgit v1.2.3