Add filter and grouping support #1735

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

Comments

Projects
None yet
4 participants

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
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?

Owner

parkr commented Nov 19, 2013

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

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.

Owner

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 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. :)

Owner

parkr commented Dec 10, 2013

@Haacked #1788 has been merged.

Haacked commented Dec 10, 2013

Yay!

bravo

Haacked commented Dec 10, 2013

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

Owner

parkr commented Dec 10, 2013

Please create a new issue :)

@mattr- mattr- closed this in #1875 Jan 4, 2014

@9peppe 9peppe referenced this issue in jekyll/jekyll-help May 11, 2014

Closed

Where are the group_by docs? :-) #42

@jekyllbot jekyllbot locked and limited conversation to collaborators Feb 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.