locomotivecms / steam

The rendering stack used by both Wagon and Station (new name of the engine). It includes the rack stack and the liquid drops/filters/tags.
MIT License
38 stars 59 forks source link

Add liquid filter `in_groups_of` #147

Closed westonganger closed 5 years ago

westonganger commented 5 years ago

Example:

{% assign grouped_list = list | in_groups_of: 4 %}

{% for group in grouped_list %}
  <div class="row">
    {% for item in group %}
       <div class="col">{{ item }}</div>
    {% endfor %}
  </div>
{% endfor %}
DonKoko commented 5 years ago

That is a really cool feature request, have wanted something like this for quite some time, however, its quite easy to achieve that already without the grouping. Here is an example:

{% assign slides = contents.activities.count | divided_by: 4 %}
{% assign offset = 0 %}
{% for slide in (1..slides) %}
  <li class="is-active orbit-slide">
    <div class="slide-wrap">
      {% for activity in contents.activities | limit: 4 | offset: offset %}
        {% include activity_card %}
      {% endfor %}
    </div>
  </li>
  {% assign offset = offset | plus: 4 %}
{% endfor %}
westonganger commented 5 years ago

Honestly that solution looks complicated. I would rather not have to come up with wizardry like that for such a common feature.

One nice thing about my solution is that, like the rails version, it is capable of padding the array with the appropriate number of elements for the last group.

DonKoko commented 5 years ago

Yes, I think we can all agreed that the solution you suggest is better. I am simply giving you a solution if you need it sooner. Take it as you will.

westonganger commented 5 years ago

Just added a documentation update for this method.

Once thats merged this issue can be closed.

DonKoko commented 5 years ago

Just saw your PR. Great work man! Excited to test it :)