Using replaceRE in scratch variable

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?

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

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") -}}

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

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.

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

I’m using a method that Bep explained on another post: 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. :confused:

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 -}}
1 Like