liver
liver

Reputation: 498

Take 3 posts at a time in Liquid (Jekyll)

This might be exceedingly obvious, but I'm making a Jekyll blog from scratch, and I'm using Shopify's Liquid.

Say I have a collection of posts:

post1, post2, post3, post4, post5, post6, post7, post8, post9, post10

I'm trying to render three posts at a time into a <div>. So it would look something like this:

<div>
    post1
    post2
    post3
</div>
<div>
    post4
    post5
    post6
</div>
<div>
    post7
    post8
    post9
</div>
<div>
    post10
</div>

How would I accomplish this in Liquid? Something like ruby's splice would seem to suffice, but I could not find how to do this in Liquid.

Upvotes: 1

Views: 108

Answers (1)

David Jacquel
David Jacquel

Reputation: 52819

{% for p in site.posts %}
   {% assign counter = forloop.index | modulo:3 %}
   {% if counter == 1 %}<div>{% endif %}
   <h3>{{ p.title }}</h3>
   {% if forloop.last or counter == 0 %}</div>{% endif %}
{% endfor %}

Upvotes: 1

Related Questions