In a shortcode for images, the final html rendered, in all places except one, writes the path separators / forward slash correctly. The exception is in the <img>
tag property of onerror
. In that property, is prints the path separators as \/
instead of /
. But the way I’m referencing the path is the same for src=
as it is for onerror=
.
So what is happening? Below is the relevant code from the shortcode (which comes from a theme geeky-hugo), followed by the resulting html:
Shortcode: image.html
{{ $image = .Page.Resources.GetMatch $imagePath }}
{{ $imageExt := path.Ext $image }}
{{ $imageWidth := $image.Width }}
{{ $imageHeight := $image.Height }}
<!-- image processing -->
{{ $options := add (add (string (.Get "w" | default $imageWidth)) "x ") (.Get "o") }}
{{ .Scratch.Set `image` ($image.Resize $options).RelPermalink }}
<!-- fallback image -->
{{ $option := add (string $imageWidth) "x" }}
{{ .Scratch.Set "image_fallback" ($image.Resize $option).RelPermalink }}
<!-- image tag -->
<img
loading="lazy"
decoding="async"
class="{{.Get `c`}} {{.Scratch.Get `image-position`}}"
width="{{.Get `w` | default $imageWidth }}"
height="{{.Get `h` | default $imageHeight}}"
src="{{.Scratch.Get `image`}}"
alt="{{.Get `alt`}}"
onerror="this.onerror=null;this.src='{{ .Scratch.Get `image_fallback` }}';"/>
As I said, the path for the src=
property gets printed correctly but the onerror=
property doesn’t. Observe:
Rendered HTML
<img
loading="lazy"
decoding="async"
class="rounded img-center"
width="970"
height="500"
src="/blog/post-1/images/testo_hu4cc21e7a863615a92cd0655f577c12f3_800270_970x0_resize_box_3.png"
alt="alter-text"
onerror="this.onerror=null;this.src='\/blog\/post-1\/images\/testo_hu4cc21e7a863615a92cd0655f577c12f3_800270_970x0_resize_box_3.png';">
If I do a little debugging and add a {{ print (.Scratch.Get "image_fallback") }}
the printed text shows the correct slashes, not the back and forward slashes:
/blog/post-1/images/testo_hu4cc21e7a863615a92cd0655f577c12f3_800270_970x0_resize_box_3.png
Unrelated Extra Credit
- What’s the difference between
.Scratch
variables and custom$image
variables? - When using
.Scratch
variables, this shortcode uses double quotes whenSet
-ting them and backticks whenGet
-ing them? Why? What’s the proper way?