Michael Manoochehri
Michael Manoochehri

Reputation: 7877

Liquid/Jekyll: How to check for no posts when one has two or more conditions?

I am having trouble understanding how I might show a "no posts exist" message for a particular conditional statement with two variables.

In this example, let's say I have a collection of "animals" - on a particular page, I'd like a section that displays "primates that are herbivores":

  {% for animal in site.animal %}
    {% if animal.species == "primate" and animal.type == "herbivore" %}

      {{ animal.content }}

    {% endif %}
  {% endfor %}

What I'd like to do is something like this (pseudocode):

{% if POSTS_EXIST_FOR_THIS_COMBO: (animal.species == "primate" and animal.type == "herbivore") %}
      {% for animal in site.animal %}
        {% if animal.species == "primate" and animal.type == "herbivore" %}
          {{ animal.content }}
        {% endif %}
      {% endfor %}
{% else %}
     There are no posts for this category.
{% endif %}

Note: This differs from examples like this, because I have two parameters to check. Can someone offer a suggestion about the syntax?

Upvotes: 1

Views: 249

Answers (1)

iurii
iurii

Reputation: 4230

I think you can do the following where you at first filter all by species=primate from site.animal and then filter by type=herbivore from that pool and then check if the result exists.

{% assign animals = site.animal | where:"species","primate" | where:"type","herbivore" %}

{% if animals %}
  {% for animal in animals %}
    {{ animal.content }}
  {% endfor %}
{% endif %}

Hope this helps.

Upvotes: 2

Related Questions