Add "like" comparison operator to the "where" function

Hi there!
I am trying to filter a list to separate it into two different lists.

I am using a (where list key "in" str) as normal and it works, however, I would need something like (where list str "in" key).

What I mean is that my key is a superset of the string I am looking for, so I want to see if the string is in the key, instead of the key in the string. Is there any way to do this in Hugo?

You cannot do this with the where function.

Range through the collection and perform a comparison in each iteration.

Thanks! I was fearing that was the case and I was ranging with an if, but wheres are beautiful! It would be great to be able to do that :slight_smile:

This would be roughly equivalent to the SQL LIKE comparison operator, e.g.,

{{ $regex := `^bar.*$`
{{ where .Pages "" "like" $regex }}

It’s an interesting idea, but I’ll admit I’ve never needed it before.

1 Like

Bumping this. I have a data template with webmentions for my whole site, and each page filters those to just the ones relevant to the current page. Webmentions are keyed by URLs, but some URLs have fragments. Instead of exact-matches with an “in” operator, having the option for a substring or regex match would make my life a lot easier.

Until then, it looks like plain iteration will be the way to go.

Please create a GitHub issue (proposal) for this. This is relatively cheap to implement, and would be useful.

Will do as soon as I replace one of my imperfect where uses with a more robust range+append flow so I have a commit to show the diff as a very clear use-case. Then I’ll use that diff in the issue.