I’ve run into an oddity with printf
and resources.Get
. I’ve searched for hours trying to get an answer to the problem and, while I found a few similar cases, none fit it exactly enough to help. (I can’t put this on a repo right now, but I hope the following will be sufficient to explain. If not, please let me know.)
Let’s say:
- For each post in
content/posts/
, I’ve created inassets/images/
a corresponding transparent PNG with that post’s title in white letters. - This is so I can overlay that PNG with
images.Overlay
on a generic background image,assets/images/background.jpg
, for social/OG card purposes. - Each transparent PNG has been named the same as the original post’s base filename except for
social-OG-text-
at the start and.png
at the end. For example: corresponding to the post filecontent/posts/2022/07/today-is-july-4th.md
is an image file,images/assets/social-text-today-is-july-4th.png
. - Thus, I want to code my
head.html
partial so that each post will get the social/OG image corresponding to the post’s filename.
But when I try this:
{{- if eq .Page.Type "posts" }}
{{- $titleText := resources.Get (printf "images/social-OG-text-%s.png" .Page.File.BaseFileName) -}}
{{- $finalFilter := (images.Overlay $titleText 0 0 ) -}}
{{- $socImg = resources.Get "images/background.jpg" -}}
{{- $socImg = $socImg | images.Filter $finalFilter -}}
{{- end -}}
… Hugo errors out (more info below) because, apparently, the printf
isn’t giving Hugo what it wants to see. On the other hand, if I hard-code a PNG file name rather using the printf
statement, it works.
In an attempt to debug this, I used the following:
{{ erroridf "%s" (resources.Get (printf "images/social-OG-text-%s.png" .Page.File.BaseFileName)) }}
…and got back repeated entries like this (substituting $MYUSERDIR
for my local user dir path):
ERROR 2022/07/21 17:09:45 render of "page" failed: "/$MYUSERDIR/hugo_site/layouts/_default/baseof.html:3:4": execute of template failed: template: posts/single.html:3:4: executing "posts/single.html" at <partial "head.html" .>: error calling partial: "/$MYUSERDIR/hugo_site/layouts/partials/head.html:58:79": execute of template failed: template: partials/head.html:58:79: executing "partials/head.html" at <.Page.File.BaseFileName>: wrong type for value; expected string; got *resources.resourceAdapter
When I ran just warnf
rather than erroridf
, I got a WARN
line for each of the PNGs I’m trying to get, complete with the full path inside assets
as it should be; for example, using the post name from my earlier example:
WARN 2022/07/21 17:39:57 images/social-OG-text-today-is-july-4th.png
(…and 200+ more like that.)
Thus, the desired PNG pathname is getting built but, for whatever reason, resources.Get
doesn’t like it. Whether done through printf
or hard-coded, it’s a string, so I don’t know why that would matter.
In short: I presume I’m not doing the printf
part correctly, but can’t figure out what I’m missing.
Will appreciate any help anyone can offer on this.