# Using replaceRE in scratch variable

**URL:** <https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849>\
**Category:** support\
**Created:** [June 3, 2017, 6:05pm UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849 "2017-06-03T18:05:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunoamaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brunoamaral/32/21144_2.png) [@brunoamaral](https://discourse.gohugo.io/u/brunoamaral)\
**Post date:** [June 3, 2017, 6:05pm UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/1 "2017-06-03T18:05:39Z")

</div>

I using the following method to serve optimized images in a few locations:

`{{ .Params.featured_image | replaceRE ".jpg|.jpeg|.JPG" "_500.jpg" }}`

One of the locations it the index.json I use for search results. So I tried the following:

```
{{- $small_image := .Params.featured_image | replaceRE ".jpg|.jpeg|.JPG" "_500.jpg" -}}
{{- $.Scratch.Add "index" (dict "title" .Title "ref" .Permalink "tags" .Params.tags "content" .Plain "image" $small_image ) -}}

```

And the result was `wrong number of args for replaceRE: want 3 got 2`.

How should I run this in the scratch context?

---

<div class="post-metadata">

**Author:** ![bschoen](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@bschoen](https://discourse.gohugo.io/u/bschoen)\
**Post date:** [June 3, 2017, 6:22pm UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/2 "2017-06-03T18:22:04Z")

</div>

The code runs without errors for me.

I can replicate the error by using a non-existing param variable. Make sure you use the right context.

Maybe your variable is defined in the global context: **$**.Params.featured\_image?

Regards,

Bas

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brunoamaral/32/21144_2.png) [@brunoamaral](https://discourse.gohugo.io/u/brunoamaral)\
**Post date:** [June 3, 2017, 7:56pm UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/3 "2017-06-03T19:56:06Z")

</div>

That’s odd. The variable is defined on the frontmatter of each post and all others, like tags, are working as they should.

I also tried to run it with an if statement to check there is a featured\_image and the same thing happens. And the range I am using is this: `{{- range where .Site.Pages "Type" "not in" (slice "page" "json") -}}`

---

<div class="post-metadata">

**Author:** ![bschoen](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@bschoen](https://discourse.gohugo.io/u/bschoen)\
**Post date:** [June 3, 2017, 9:51pm UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/4 "2017-06-03T21:51:34Z")

</div>

Odd indeed. I checked again. If the param doesn’t exist in a content \*.md frontmatter that is rendered by the template, it errors.

What check did you use? This is working for me:

```
{{ if isset .Params "featured_image"}}
    {{- $small_image := $.Params.featured_image | replaceRE ".jpg|.jpeg|.JPG" "_500.jpg" -}}
    {{- $.Scratch.Set "index" (dict "title" $.Title "ref" $.Permalink "tags" $.Params.tags "content" $.Plain "image" $small_image ) -}}
{{ end }}

```

OR

```
{{ with .Params.featured_image}}
    {{- $small_image := . | replaceRE ".jpg|.jpeg|.JPG" "_500.jpg" -}}
    {{- $.Scratch.Set "index" (dict "title" $.Title "ref" $.Permalink "tags" $.Params.tags "content" $.Plain "image" $small_image ) -}}
{{ end }}

```

**_Note:_** I used `$.Scratch.Set` instead of `$.Scratch.Add`. Because (according to the docs) : `For single values, Add accepts values that support Go’s + operator. If the first Add for a key is an array or slice, the following adds will be appended to that list.`

Regards,

Bas

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brunoamaral/32/21144_2.png) [@brunoamaral](https://discourse.gohugo.io/u/brunoamaral)\
**Post date:** [June 4, 2017, 9:38am UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/5 "2017-06-04T09:38:58Z")

</div>

I was checking if the lenght was larger than zero.

This does solve the error, but the index.json that it ouputs is still showing the original value for featured\_image.

Makes me believe there is more to this than the syntax error.

---

<div class="post-metadata">

**Author:** ![bschoen](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@bschoen](https://discourse.gohugo.io/u/bschoen)\
**Post date:** [June 4, 2017, 10:46am UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/6 "2017-06-04T10:46:26Z")

</div>

How are you rendering your index.json?

Just to confim the scratch contains the “\*\_500.jpg” you could debug with:

```
{{ with $.Scratch.Get "index" }}
    {{ printf "%#s" .image }}
{{ end }}

```

Regards,

Bas

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brunoamaral/32/21144_2.png) [@brunoamaral](https://discourse.gohugo.io/u/brunoamaral)\
**Post date:** [June 11, 2017, 6:33pm UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/7 "2017-06-11T18:33:51Z")

</div>

I’m using a method that Bep explained on another post: [https://discuss.gohugo.io/t/simple-json-site-index-in-hugo/2854](https://discuss.gohugo.io/t/simple-json-site-index-in-hugo/2854)

The only difference is that I am not using the index.ace file.

This needed to stay on hold for a while but I will eventually get back to it. Or simply give up and add the thumbnail to the post frontmatter. 😕

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brunoamaral/32/21144_2.png) [@brunoamaral](https://discourse.gohugo.io/u/brunoamaral)\
**Post date:** [August 15, 2017, 1:36pm UTC](https://discourse.gohugo.io/t/using-replacere-in-scratch-variable/6849/8 "2017-08-15T13:36:21Z")

</div>

Today I figured out how to make this work. There were two problems:

1. The code was breaking because sometimes the params.featured\_image variable was null and replaceRE missed an argument

2. in order to check for it and set a variable to use, I needed to work inside the “if” loop.

The final code is this:

```
{{- $.Scratch.Add "index" slice -}}
{{- range where .Site.Pages "Type" "not in" (slice "page" "json") -}}
		{{ if .Params.featured_image }}
			{{- $thumbnail := ( .Params.featured_image | replaceRE ".jpg|.jpeg|.JPG" "_500.jpg") -}}
			{{- $.Scratch.Add "index" (dict "title" .Title "ref" .Permalink "tags" .Params.tags "content" .Plain "image" $thumbnail ) -}}
		{{else}}
			{{- $.Scratch.Add "index" (dict "title" .Title "ref" .Permalink "tags" .Params.tags "content" .Plain "image" .Params.featured_image ) -}}
		{{- end -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}
```
