So I’m building a website, and I’m so very frustrated. I want to be able to have a default featured image if none is specified in the frontmatter of a post.
Now here’s the frustrating part: Whenever I do this I get the following error:
Failed to render pages: render of "home" failed: execute of template failed: template: index.html:5:12: executing "index.html" at <partial "header.html" .>: error calling partial: "{Path-to-my-site}\layouts\partials\header.html:57:110": execute of template failed: template: partials/header.html:57:110: executing "partials/header.html" at <$image>: nil pointer evaluating resource.Resource.Resize
If I understand, this error is saying I didn’t provide a valid URL to the resource. But if I just print my image variable I get the correct response:
/images/bison.jpg
and if I specify that image path literally in my code (such as {{ ((resources.Get "/images/bison.jpg").Resize "500x jpg q75").RelPermalink }}) it works fine.
Prior to v0.48.0 (August 29, 2018) you could not assign a new value to a variable once it had been initialized. This was due to a Golang limitation that took over three years to address.
The Hugo “Scratch” was created to work around this limitation. Unfortunately, this outdated construct still exists in examples, tutorials, and themes.
There are valid reasons to use a Scratch, but this isn’t one of them.
Failed to render pages: render of "home" failed: execute of template failed: template: index.html:5:12: executing "index.html" at <partial "header.html" .>: error calling partial: "[path-to-site]\layouts\partials\header.html:52:105": execute of template failed: template: partials/header.html:52:105: executing "partials/header.html" at <resources.Get>: error calling Get: dirs not supported resource types: &{0xc0008c8ff0 0xc0001d6a00}
Only the scratch method works works for me, and only when I write it as a negative (as in the solution above). I tried checking to see what version of Go I have installed, and I was on 1.16.x Ran upgrade from CLi and got to 1.17.3, but I don’t think that’s the issue. I’ll update this thread once Go installs the upgrade and I rerun.
Update
I’ve updated Go to 1.17.3 and Hugo to 0.89.1 and this I can still only get this to work with .Scratch, and only when the logic is done in the aforementioned order (by negation).
I’m pretty sure this is a bug.
But, in case it makes a difference, I’m doing all of this from a partial.