Issues using `Page.Resources.Get` within a shortcode

I’m trying to make a shortcode to downscale images so that a smaller preview version is displayed on the page.

The (truncated) directory tree is as follows:

.
├── content
│   ├── assets
│   │   ├── al2f_button_html.png
│   │   ├── css_is_difficult.gif
│   │   └── pixel.css
│   ├── home.md
│   ├── posts
│   │   ├── harlock
│   │   │   ├── 2025-03-06_Captain-Harlock-Draft_by_al2f_2500x1407.png
│   │   │   ├── 2025-03-06_Captain-Harlock-Draft_by_al2f_5000x2813.png
│   │   │   ├── 2025-03-07_Captain-Harlock_by_al2f_2500x1407.png
│   │   │   ├── 2025-03-07_Captain-Harlock_by_al2f_5000x2813.png
│   │   │   └── index.md
│   │   ├── _index.md
│   ├── programs.md
│   ├── projects.md
│   └── resources.md
├── layouts
│   ├── 404.html
│   ├── index.html
│   └── shortcodes
│       └── downsimg.html
├── themes
│   ├── hugo-video
│   └── simplog

Here is the failing downsimg.html:

{{ $preview := $original.Resize "636x" }}
{{ $alt := .Get 1 }}
{{ $maxw := .Get "max_width" | default 1500 }}
{{ $maxh := .Get "max_height" | default 800 }}

{{ $preview = $original }}

{{ if gt $maxw $original.width }}
	{{ $preview = $original.Resize ( printf "%sx" $maxw ) }}
{{ else if gt $maxh $original.height }}
	{{ $preview = $original.Resize ( printf "x%s" $maxh ) }}
{{end}}

{{ if (eq $original.Width $preview.Width) and (eq $original.Height $preview.Height) }}
<img src="{{ $original.RelPermalink }}" width="{{ $original.Width }}" alt="{{ $alt }}">
{{ else }}
<a href="{{ $original.RelPermalink }}" target="_blank" title="{{ $alt }}">
	<img src="{{ $preview.RelPermalink }}" width="{{ $preview.Width }}" alt="{{ $alt }}">
	<p>Resized to {{$preview.Width}}x{{$preview.Height}}. <a href="{{$original.RelPermalink}}">View original</a> ({{$original.Width}}x{{$original.Height}})</p>
</a>
{{ end }}

Here is a simpler version which I’m using for debugging:

{{ $src := .Get "src" | default (.Get 0) }}
{{ $original := .Page.Resources}}

<h1>src '{{$src}}'</h1>
<h1>rsc '{{.Page.Resources}}'</h1>
<h1>mtc '{{ .Page.Resources.Get "$src" }}'</h1>

Here is index.md:

---
title: "Captain Harlock artwork"
date: 2025-03-31T12:11:21+10:30
draft: false
categories:
 - art
tags:
 - krita
 - captain harlock
---

{{< downsimg src=2025-03-07_Captain-Harlock-Draft_by_al2f_5000x2813.png >}}

The html displays the 3 headings, however the last ‘mtc’ (match) heading is a pair of empty quotes:

src ‘2025-03-07_Captain-Harlock-Draft_by_al2f_5000x2813.png’
rsc ‘[2025-03-06_captain-harlock-draft_by_al2f_2500x1407.png 2025-03-06_captain-harlock-draft_by_al2f_5000x2813.png 2025-03-07_captain-harlock_by_al2f_2500x1407.png 2025-03-07_captain-harlock_by_al2f_5000x2813.png]’
mtc ‘’

Unsurprisingly, trying to run <h1>mtc '{{(.Page.Resources.Get "$src").Permalink}}'</h1> gives the following error: execute of template failed at <"$src">: nil pointer evaluating resource.Resource.Permalink

What am I doing wrong? Why doesn’t .Page.Resources.Get return anything?

Might be a typo in the image path, Checking…

It looks like no quotes are needed around $src in .Page.Resources.Get "$src".

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.