Issue using .Where with .In

I have a .yaml file in my .Site.Data that has URLs to all of my JS files so that I can logically include or exclude the file. An example of an entry:

- title: Base Scripts
  url: js/_scripts.js
  enabled: true
  order: 4
  includeAtTop: true
  includeAtBottom: false
  environments: ["dev", "prod"]
  condition: Params.scriptsToInclude.scripts

I am trying to select only those files that are to be included at the top or the file and have "dev" as one of the environments values. I have attempted:

Attempt 1
{{ range sort (where (where $files "includeAtTop" "eq" true) "environments" "in" "dev" ) "order" }}

Attempt 2
{{ range sort (where (where $files "includeAtTop" "eq" true) in "environments" "dev" ) "order" }}

With option 1, none of them evaluates to true. With option 2, I get the error execute of template failed at <in>: wrong number of args for in: want 2 got 0

I’m fairly certain it has to do with me attempting to use where in conjunction with in. But I’d like to avoid wrapping a condition around another condition such as

{{ range sort (where $files "includeAtTop" "eq" true) "order" }}
        {{if in .environments "prod"}}
            ...some code
        {{end}}
{{ end }}

Is using .in possible when using .where?

You need to use the intersect operator.

See example:
https://gohugo.io/functions/collections/where/#intersection-comparison

This worked, with the addition of first converting the string dev to a slice

{{ range sort (where (where $files "includeAtTop" "eq" true) "environments" "intersect" (slice "dev") ) "order" }}
  ... some code
{{end}}

Thanks

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.