Copying a resource in page bundles

Is there a .Resources.Copy?

Is there a .Resources.Copy?

No. But you can you use the resources.Copy method with a page resource.

{{ with .Resources.Get "a.jpg" }}
  {{ with .Resize "200x webp" }}
    {{ with . | resources.Copy "b.jpg" }}
      <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ end }}
  {{ end }}
{{ end }}

But that will not work if you don’t process the image somehow. For example, this silently fails:

{{ with .Resources.Get "a.jpg" }}
  {{ with . | resources.Copy "b.jpg" }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

But it works as expected with a global resource:

{{ with resources.Get "a.jpg" }}
  {{ with . | resources.Copy "b.jpg" }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

So, there’s room for improvement with resources.Copy (which I will log). Or perhaps we create a parallel .Resources.Copy method (even if does it exactly what resources.Copy does).

To work around the problem described above, use this approach (credit @pamubay):

{{ with .Resources.Get "a.jpg" }}
  {{ with .Content | resources.FromString "b.jpg" }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

https://github.com/gohugoio/hugo/issues/10584

Impressive.

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