Search code examples
filteroperatorsjekyll

and/or on a where_exp expression on Jekyll


I'm trying to use where_exp to filter Jekyll pages based on two categories using or operator:

{% assign sortedPages = site.pages | sort:"date" | reverse | where_exp:"page","page.categories contains 'design-pattern'" %}

But I'm getting this error:

Expected end_of_string but found pipe

Ar or/and operators really supported? I can't figure out how to filter pages using where_exp as shown in my code snippet.


Solution

  • I've managed to solve my issue with some workarounds... It's not a very elegant solution, but as Jekyll isn't about creating actual software architecture... For now, it works flawlessly:

    {% assign patterns = "" | split,"" %}
    
    {% for page in site.pages %}
    {% if page.categories contains "design-pattern" or page.categories contains "anti-pattern" %}
    {% assign patterns = patterns | push:page %}
    {% endif %}
    {% endfor %}
    
    {% assign sortedPages = patterns | sort:"date" | reverse %}
    
    1. I create an empty array.
    2. I loop over all pages and I filter them by two possible categories.
    3. I push pages that fulfill the whole condition.
    4. Once I get the filtered page array, I sort and reverse it.