yogthos/Selmer

Confused: multiple filters don’t seem to be working on a collection

aviflax opened this issue · 8 comments

Sorry if this is a n00b question, but I am a n00b.

I’ve created a few custom filters for sorting and filtering collections.

I think what I’m seeing now is that if I apply two filters to a collection, the result seems to be applied only to the original collection, rather than the result of the first filter.

For example, my tag looks something like:

{% for foo in my.coll|my-filter|my-sort %}

The result seems to be equivalent to my.coll|my-sort — it seems as though the results of the first filter are being thrown away?

I’m sure this is PEBKAC — what am I missing?

Thanks!

You're right, the filters don't chain for collections at the moment. I'd call it a bug since filters chain otherwise. I'll try take a look at fixing this when I get a chance. In the meantime, you'd have to compose the filters into a single filter that would do all the transformations in one shot.

😌 its actually a relief to know that I’m not crazy. Thanks!

I’d be happy to take a look at maybe contributing a fix for this, if you maybe want to point me in the right direction?

If you don't mind taking a look that would be great, and compile-filter-body would likely be the best place to start. That's where the filters get parsed out and mapped to the respective functions.

Thanks! I’ll try to take a look, but it might be a week or two before I can get to it.


In the meantime, I wonder if you could once again help me refine my mental model of Selmer… I thought I might be able to work around this bug by using with to assign a filtered collection to a variable, and then inside of that with I could apply another filter to the collection.

e.g.:

{% with blips = radar.blips|filter-by:ring:@ring %}
  {% for tech-name,blip in blips|sort-by-keys %}

but no dice. In fact in this case my variable seems to be empty? If I remove the second filter, in the for, I still get nothing — blips seems to be empty. When I remove both filters, however, the variable does seem to hold the entire collection. So variables can refer to collections — but not filtered collections?

Is this possibly the same bug? Or a different bug? Or am I missing something?

Thanks!

No worries, and it sounds like this is likely related as well, but I'd have to take a closer look to be sure.

👋 I had a hiatus from working on projects that used Selmer for a couple of years, but I just started working on another one, and maybe I’m confused or missing something, but it seems now like this is working? Has something changed in the meantime?

I guess it's possible this got fixed under some other ticket and this issue just didn't get updated?

(~/clojure)-(!2007)-> clj -Sdeps '{:deps {selmer/selmer {:mvn/version "RELEASE"}}}'
Downloading: selmer/selmer/maven-metadata.xml from clojars
Clojure 1.11.1
user=> (require '[selmer.parser :as p])
nil
user=> (p/render "{% for i in data|take:6|drop:3 %}{{i}}:{% endfor %}" {:data (range 20)})
"3:4:5:"
user=>

Yeah, looks like this got fixed by another refactor. Sounds like we can close this then.