Anton Shuvalov
Anton Shuvalov

Reputation: 3671

loop through filtered collection in jekyll

I try to do things like this in my post:

<ul class="articles-list">
  {% for post in site.posts | where:'group', post.group %}
    <div data-scroll-reveal="enter ease 0">
      {% include article-snippet.html %}
    </div>
  {% endfor %}
</ul>

but it loops through all my collection instead the only loops posts with special group.

Upvotes: 17

Views: 7521

Answers (2)

Mitja Bezenšek
Mitja Bezenšek

Reputation: 2533

According to liquid documentation about filters they should be used inside output tags {{ and }}.

You can maybe try an if statement:

{% for post in site.posts %}
    {% if post.group == 'group' %}
        <div data-scroll-reveal="enter ease 0">
            {% include article-snippet.html %}
        </div>
    {% endif %}
{% endfor %}

Also you should use the where filter a bit differently. Something like this:

{{ site.members | where:"graduation_year","2014" }}

This says select site members whose graduation_year is 2014. You don't need to specify that it should filter members, the first part implicitly states that.

Upvotes: 4

David Jacquel
David Jacquel

Reputation: 52809

You cannot use where filter in a loop.

But you can do :

{% assign posts = site.posts | where:'group', post.group %}

<ul class="articles-list">
  {% for post in posts %}
    <div data-scroll-reveal="enter ease 0">
      {% include article-snippet.html %}
    </div>
  {% endfor %}
</ul>

Upvotes: 26

Related Questions