resources.Get from Parameter

I am playing with the new resources.Get function thinking that maybe I can use it as an alternative to Page Bundles for older projects that I haven’t converted yet.

In my config I entered assetDir = "static" (pleased to confirm that this works, creating symlinks under /assets/ didn’t work for me).

Now in each post I have the following frontmatter parameter image= "/images/1.jpg"

In the template I first tried the following:

{{ $path := .Params.image }}
{{ $thumb := resources.Get $path | safeHTML  }}

And <img src="{{ $thumb | absURL }}"/>

However the above is outputting the Go templates unsafe operation error img src="#ZqotmplZ"

So after searching a bit I tried the following based on this old forum post:

{{ $thumb := (printf "/%s" (resources.Get .Params.image)) | safeHTML  }}

Now I’m not getting the Go templates error but instead the URL is printed wrong:

http://localhost:1313/Resource%28image:%20images/1.jpg%29

Also tried variations with safeURL and safeHTMLAttr but I got the same result.

Any pointers?

{{ $thumb := resources.Get $path  }}
<img src="{{ $thumb.Permalink }}"/>
3 Likes

Ah! Thank you! :tada:

For reference’s sake here is the final working snippet:

    {{ $path := .Params.image }}
    {{ $thumb := resources.Get $path  }}

  <img src="{{ $thumb.Permalink }}"/>

A small side note:
The use of safeHTML in {{ $thumb := resources.Get $path | safeHTML }} is not needed (it was throwing a console error).

A final note from me, a little simplified, but you can process the image you get via Get:

 {{ $image := resources.Get $path  }}
{{ $thumb := $image.Fill "100x100" }}
2 Likes

Thanks! I already did the resize.

Also I am amazed with how quickly Hugo generated 65 thumbnails. It only took a couple of seconds.

1 Like
  • Hugo’s image processing is relatively slow (compared to imagemagick)
  • But you get a performance win as Hugo is good at keeping all your CPU cores busy
  • Also, once generated, we will reuse the thumbnails inside /resources if present
1 Like