From 0a61815dcbe50f289cc40085b015646e38f8fae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20K=C3=A1n=C4=9B?= Date: Thu, 16 Apr 2020 23:14:46 +0200 Subject: [PATCH] Add blogPhoto shortcode This shortcode is intended to be used in blogposts to include photos. It downscales them and uses srcset to serve appropriate sizes to the browsers. --- .../patek/layouts/shortcodes/blogPhoto.html | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 themes/patek/layouts/shortcodes/blogPhoto.html diff --git a/themes/patek/layouts/shortcodes/blogPhoto.html b/themes/patek/layouts/shortcodes/blogPhoto.html new file mode 100644 index 0000000..3c1396e --- /dev/null +++ b/themes/patek/layouts/shortcodes/blogPhoto.html @@ -0,0 +1,29 @@ +{{- /* +Usage: + +{{< blogPhoto "filename_relative_to_page.ext" "Alt text" >}} + +or + +{{< blogPhoto src="filename_relative_to_page.ext" alt="Alt text" >}} + +*/ -}} +{{- $imageName := false -}} +{{- $alt := false -}} +{{- if .IsNamedParams -}} + {{- $imageName = .Get "src" | default nil -}} + {{- $alt = .Get "alt" | default nil -}} +{{- else -}} + {{- $imageName = .Get 0 -}} + {{- $alt = .Get 1 -}} +{{- end -}} +{{- if (eq $imageName nil) }}{{ errorf "Missing image name in %s" .Position }}{{ end -}} +{{- if (eq $alt nil) }}{{ errorf "Missing alt for image %s in %s" $imageName .Position }}{{ end -}} + +{{- $img := .Page.Resources.GetMatch (printf "%s" $imageName) | default nil -}} +{{- if (eq $img nil) }}{{ errorf "Image %s not found in %s" $imageName .Position }}{{ end -}} +{{- $resizedFullHD := $img.Fit "1920x1080 q50" -}} +{{- $resizedHD := $img.Fit "1280x720 q50" -}} +{{- $resizedSD := $img.Fit "640x360 q50" -}} + +{{ $alt }}