[Solved] Problem with "where in" construct


I want to paginate over a list of files in a certain subdirectory.
This is what I am trying to do:

{{ range $index, $element := (where (where .Data.Pages "Type" "eq" "guideline") ".File.Dir" "in" "/text/" ) }}

It should give me a list of guidelines stored in the “text” directory.
However, nothing is returned.

When I try this (including the full path), the files are returned as expected:

{{ range $index, $element := (where (where .Data.Pages "Type" "eq" "guideline") ".File.Dir" "in" "guidelines/EN/text/" ) }}

Am I using the “in” function wrong or is it broken and does not recognize substrings anymore?

I already tried what was recommended in this thread, but it didn’t help sadly.



Hi Leo,

Looks like in can be used in two different context. First, as operator in where condition. In this context it checks if left argument exists in array on the right, so you could use it like following.

where .Data.Pages ".File.Dir" "in" (slice "guidelines/EN/text/" "guidelines/DE/text/")

Second scenario is logical function comparing substrings (in "big main string" "substring")

I’m not sure if where can be convinced somehow to use logical functions but even if it can’t there is easy workaround.

{{ range $index, $element := where .Data.Pages "Type" "eq" "guideline" }}
    {{ if (in $element.File.Dir "/text/")  }}
        Do your magic stuff here

Best Regards,

Hey Tarmo, thanks for your reply,
yes, you are right in this special case the condition within the range would work, I am gonna use that.
In other cases that I need to handle though, the condition needs to be within the range itself. To be more specific: when working with the paginator I can’t have the if, because that might render empty pages.

Thank you for your reply, I am looking forward to v0.25 then to make it proper.

1 Like