hugo
hugo copied to clipboard
Add resources.IsProcessableImage
This is slightly related to https://github.com/gohugoio/hugo/pull/11688
Note that I'm not totally sure about this ... There may be an alternative fix lurking around for this somewhere.
- Currently
$resources.ByType
means filter by MIME main type. - The problem with this for type
image
is that most people expect the images returned to have a.Width
and to be resizeable etc.
We have a few options:
- We could make
$resources.ByType 'image'
return only images that's processable. - We could add some $resource.IsProcessableImage` ... or something less ugly.
Not sure.
I'll vote for the $resource.IsProcessableImage
, I think $resources.ByType 'image'
should return all images regardless if it's processable, such as SVG, beside this approach probably bring breaking changes.
I agree with @razonyang. I think this is fine:
{{ range resources.ByType "image" }}
{{ with and .IsProcessableImage (.Process "resize 200x webp") }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
Using a method as shown above seems a little cleaner to me. The alternative is:
{{ range resources.ByType "image" }}
{{ with and (resources.IsProcessableImage .) (.Process "resize 200x webp") }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
I have been thinking about this, and on the implementation level this would be much easier/cleaner if we make it a function instead of a method -- then it would just be a matter of checking if the type implements an interface, which is a pattern we could extend on if needed, e.g:
-
resources.IsProcessableImage
-
resources.HasWidth
...
Also, having $page.IsProcessableImage
always return false isnt't great when we want to document this down the line and then gets $page.IsProcessableVideo
etc..
I upvote @razonyang's suggestion.