resources.Get throws and has no way to check if the item exists

For top level resources from the assets folder, there seems to be know way to know if a resource exists. If we use resources.Get('image/logo.png') and logo.png does not exist, it throws rather than returning empty. Page resources, .Resources.Get works correctly. Is there a way to detect if image/logo.png exists. Similarly resource.GetMatch also does not seem to exist for top level resources.

Try something like this, which uses fileExists

{{ $asset := "image/logo.png" }}

{{ if (fileExists (printf "assets/%s" $asset)) }}
  {{ resources.Get $asset }}
{{ else }}
  {{ printf "%s does not exist" $asset }}
{{ end }}
2 Likes

What do you mean by “throws”?

Here is the code. Note that favicon.png does not exist.

{{- with resources.Get "image/favicon.png" -}}
  <link rel="apple-touch-icon-precomposed" href="{{ (.Resize " 180x180 ").RelPermalink }}" />
  <link rel="icon" type="image/png" href="{{ (.Resize " 196x196 ").RelPermalink }}" />
  {{- end -}}

Result:

@zwbetz thanks will try it out.

OK, yes that is not … ideal:

The workaround from @zwbetz should work, though.

1 Like

Not related to bep’s issue, the error is about a missing logo.png, not a favicon.png.

You should have a remaining case elsewhere

Sorry about that. I had a similar case with logo.png