# How to check for returned value from .Resources.GetMatch?

**URL:** <https://discourse.gohugo.io/t/how-to-check-for-returned-value-from-resources-getmatch/11797>\
**Category:** support\
**Created:** [May 1, 2018, 4:22pm UTC](https://discourse.gohugo.io/t/how-to-check-for-returned-value-from-resources-getmatch/11797 "2018-05-01T16:22:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![frangipani](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frangipani/32/5558_2.png) [@frangipani](https://discourse.gohugo.io/u/frangipani)\
**Post date:** [May 1, 2018, 4:22pm UTC](https://discourse.gohugo.io/t/how-to-check-for-returned-value-from-resources-getmatch/11797/1 "2018-05-01T16:22:44Z")

</div>

I’m trying to use an image page resource for thumbnail generation. If there’s no image with such name, then fallback to another image.

```auto
{{ $thumb := .Resources.GetMatch "thumb.*" }}

{{ if not $thumb }}
    {{ $thumb := .Resources.GetMatch "0.*" }}
{{ end }}

{{ $thumb := $thumb.Resize "500x" }}

```

Apparently, this is not the way to do it as I don’t get the fallback image (0.jpg) if thumb.jpg is missing.

Either this is not the right way to check if $thumb contains a resource, or whatever happens inside the conditional statement is not usable outside of it.

Any tips? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [May 1, 2018, 4:56pm UTC](https://discourse.gohugo.io/t/how-to-check-for-returned-value-from-resources-getmatch/11797/2 "2018-05-01T16:56:50Z")

</div>

> [@frangipani](#):
>
> ce, or whatever happens inside the conditional statement is not usable outside of it.

Spot on. This will be fixed in Go 1.11, but until then you need to use .Scratch (search the docs).

---

<div class="post-metadata">

**Author:** ![frangipani](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frangipani/32/5558_2.png) [@frangipani](https://discourse.gohugo.io/u/frangipani)\
**Post date:** [May 1, 2018, 6:27pm UTC](https://discourse.gohugo.io/t/how-to-check-for-returned-value-from-resources-getmatch/11797/3 "2018-05-01T18:27:22Z")

</div>

Thanks, I’d read something about it but thought it’s irrelevant for “custom” page variables.

For anybody interested - not sure this is the way to use .Scratch but here’s what worked for me:

```auto
{{ $thumb := .Resources.GetMatch "thumb.*" }}
{{ .Scratch.Set "thumbscratch" $thumb }}

{{ if not $thumb }}
    {{ $thumb := .Resources.GetMatch "0.*" }}
    {{ .Scratch.Set "thumbscratch" $thumb }}
{{ end }}

{{ $thumb := .Scratch.Get "thumbscratch" }}

{{ $thumb := $thumb.Resize "500x" }}

```

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [May 1, 2018, 6:34pm UTC](https://discourse.gohugo.io/t/how-to-check-for-returned-value-from-resources-getmatch/11797/4 "2018-05-01T18:34:20Z")

</div>

> [@frangipani](#):
>
> Any tips? Thanks in advance.

Based on the [glob examples](https://github.com/gobwas/glob/blob/master/readme.md) for the library that `hugo` uses for `.GetMatch`, it should be possible to replace that whole thing with:

```auto
{{ with .Resources }}
    {{ with .GetMatch "{thumb.*,0.*}" }}
        {{ $thumb := .Resize "500x" }}
        <!-- more stuff -->
    {{ end }}
{{ end }}

```

_I like to use `with` as much as possible._

---

<div class="post-metadata">

**Author:** ![frangipani](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frangipani/32/5558_2.png) [@frangipani](https://discourse.gohugo.io/u/frangipani)\
**Post date:** [May 1, 2018, 6:46pm UTC](https://discourse.gohugo.io/t/how-to-check-for-returned-value-from-resources-getmatch/11797/5 "2018-05-01T18:46:46Z")

</div>

Wow, this looks so much better! And yes, it does work.
