Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 951
Anton Shuvalov Avatar asked Aug 03 '14 10:08

Anton Shuvalov


2 Answers

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>
like image 105
David Jacquel Avatar answered Nov 18 '22 19:11

David Jacquel


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.

like image 44
Mitja Bezenšek Avatar answered Nov 18 '22 20:11

Mitja Bezenšek