Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter and grouping support #1735

Closed
Haacked opened this Issue Nov 19, 2013 · 9 comments

Comments

Projects
None yet
4 participants
@Haacked
Copy link

Haacked commented Nov 19, 2013

I had a case where I wanted to group a bunch of pages by a property. Here's a naive example of the type of code I would want to write:

{% for group in site.pages.filter(:enabled).group_by(:someProperty) %}
   <h2>{{ group.Name }}</h2>
   <ul>
   {% for item in group.Items %}
      <li>{{ item.FooBar }}</li>
    {% endfor %}
  </ul>
{% endfor %}

This could be a better way to handle grouping blog posts by dates too.

  • group_by
  • where
@benbalter

This comment has been minimized.

Copy link
Contributor

benbalter commented Nov 19, 2013

Since jsonify dropped, the ability to filter would be absolutely killer. e.g., {{ site.pages | filter:"layout","license" | jsonify }} Perhaps a where filter would be a more Jekyll-y way?

@parkr

This comment has been minimized.

Copy link
Member

parkr commented Nov 19, 2013

This is a great idea! Would you filter only by category, or by anything in page.data?

@benbalter

This comment has been minimized.

Copy link
Contributor

benbalter commented Nov 19, 2013

In my head, I was thinking it was similar to ruby's select behavior, or underscore's where. Iterate through an array of objects and return those objects where the given key matches the specified value.

@parkr

This comment has been minimized.

Copy link
Member

parkr commented Nov 19, 2013

@benbalter Hm, I think @Haacked's idea of "group_by" would make more sense in this case, as it'd group by a key. Otherwise, we'd have to know both the key and the value. Syntax is doable, but would have to be like filter: "layout => license" and parsed as filter the given array such that only objects whose layout key is "license". I'd probably add two filters here:

  • group_by
  • filter (filter only by ==, or also by include??)
@Haacked

This comment has been minimized.

Copy link
Author

Haacked commented Nov 19, 2013

Would you filter only by category, or by anything in page.data?

Ideally anything in page.data. That'd make it much more powerful.

Also, I don't think select and where capture the group_by semantics. group_by is effectively an aggregation function. Not sure what that corresponds to in Ruby. Sorry. :)

@parkr

This comment has been minimized.

Copy link
Member

parkr commented Dec 10, 2013

@Haacked #1788 has been merged.

@Haacked

This comment has been minimized.

Copy link
Author

Haacked commented Dec 10, 2013

Yay!

bravo

@Haacked

This comment has been minimized.

Copy link
Author

Haacked commented Dec 10, 2013

Someone on the twitters asked about sorting. I added it here, but should I create a new issue? Thoughts?

@parkr

This comment has been minimized.

Copy link
Member

parkr commented Dec 10, 2013

Please create a new issue :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.