Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this Jekyll Liquid where filter filter?

Tags:

ruby

jekyll

I am trying to output a list of blog posts for a certain author. I tried this where Jekyll filter:

{% for post in (site.posts | where:"author", "mike") %}
  {{ post.title }}
{% endfor %}

But it outputs every post. I'm not clear what I'm doing wrong.

like image 433
Justin Avatar asked Jul 11 '14 15:07

Justin


2 Answers

You need to do an assign first for the filtered items

{% assign posts = site.posts | where:"author", "mike" %}
{% for post in posts %}  
   {{ post.title }}
{% endfor %}
like image 64
Chetabahana Avatar answered Sep 23 '22 12:09

Chetabahana


Supposing that your post author is in your front matter, like this :

---
author: toto
...
---

If you want two last post by author == toto, just do :

{% assign counter = 0 %}
{% assign maxPostCount = 2 %}
<ul>
{% for post in site.posts %}
  {% if post.author == 'toto' and counter < maxPostCount %}
    {% assign counter=counter | plus:1 %}
    <li>{{ counter }} - {{ post.title }}</li>
  {% endif %}
{% endfor %}
</ul>

Et hop !

EDIT : And another solution using the where filter instead of the if clause :

{% assign posts = site.posts | where: "author", "toto" %}
{% assign counter2 = 0 %}
{% assign maxPostCount2 = 3 %}
<ul>
{% for post in posts %}
  {% if counter2 < maxPostCount2 %}
    {% assign counter2=counter2 | plus:1 %}
    <li>{{ counter2 }} - {{ post.title }}</li>
  {% endif %}
{% endfor %}
</ul>

RE-EDIT: Justin is right I don't need my two vars (counter2 and maxPostCount2), I can use Liquid for loop limit:n option.

{% assign posts = site.posts | where: "author", "toto" %}
<ul>
{% for post in posts limit:3 %}
  <Ol>{{ post.title }}</ol>
{% endfor %}
</ul>

Better !

like image 12
David Jacquel Avatar answered Sep 22 '22 12:09

David Jacquel