How to use images from page bundle and static dir for cover images

Hello there. I am choosing SSG (Hugo, Gatsby, Zola) and Hugo looks very promising to me. Unfortunately, there is one problem with cover partial. I want to use images from the static folder and page bundle simultaneously through one front matter param. It should work something like that: I fill cover url front-matter param and if such image exists in the page bundle, then use it, otherwise try to get the fallback image with the same relpath from static folder.


cover: super-duper-img.png


<site-root>/content/post/example-post/super-duper-img.png # use by default
<site-root>/static/super-duper-img.png # fallback to static if no image in the page bundle

p.s. sorry for bad english

Place your fallback image in assets/img/ instead of static/, and then you can do this:

{{- with .Params.cover -}}
  {{/* Use page resource if it exists. */}}
  {{- with $.Resources.GetMatch . -}}
    <img alt="" src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">
  {{- else -}}
    {{/* Use global resource if it exists. */}}
    {{- with resources.GetMatch (printf "img/%s" . ) -}}
      <img alt="" src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">
    {{- end -}}
  {{- end -}}
{{- end -}}

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