If in category and case insensitive

I want to check to see if a value is present in the categories. I have the below code.

{{ if isset .Params "categories" }}
{{ if in .Params.categories "TEST" }}
     Do Stuff
{{ end }}
{{ end }}

This code works, but now I want to make it case insensitive.

I tried:
{{ if in (lower .Params.categories) (lower "TEST") }}

But it results in:

<lower .Params.categories>: error calling lower: unable to cast []string{"Test"} of type []string to string

Any ideas what is going on?

.Params.categories is a string array or an array of strings if you will []string{"Test"} and not a string string.

In order to lower all the items in your array, you can use apply which runs a given function on items in an array and returns the array with the returned values.

{{ $lower_categories := apply .Params.categories "lower" }}
{{ if (in $lower_categories (lower "TEST")) }}
[...]
{{ end }}
1 Like

@regis thank you for the help.

I get:
<apply .Params.categories "lower">: error calling apply: runtime error: index out of range [0] with length 0

Iā€™m using this in a partial if that matters.

Sure. Well before calling apply it seems you need to make sure your array is not empty.
You can set a default empty array and overwrite if with succeeds.

{{ $lower_categories := slice }}
{{ with .Params.categories }}
  {{ $lower_categories := apply . "lower" }}
{{ end }}
{{ if (in $lower_categories (lower "TEST")) }}
[...]
{{ end }}
1 Like