Julia Ebert
Julia Ebert

Reputation: 1623

Jekyll: check if post content empty

I have a series of posts in a Jekyll project where some of have just a title and some have a title and content. I want to do different things with the post in each case. For example:

{% for post in site.categories.publications %}
    {{ post.title }}
    {% if post.content == "" or post.content == nil or post.content == blank %}
        <p>Nothing here.</p>
    {% else %}
        {{ post.content }}
    {% endif %}
{% endfor %}

But the if statement doesn't actually catch the empty posts. I based my conditions on this page, but none of the 3 possibilities catch the empty posts. Any ideas on how to handle these posts?

Upvotes: 10

Views: 6641

Answers (2)

Mike Slinn
Mike Slinn

Reputation: 8407

I prefer to test using unless:

{% assign content = page.content | strip_newlines %}
{% unless content == null or content == "" %}
  {{ content }}
{% endunless %}

Upvotes: 1

David Jacquel
David Jacquel

Reputation: 52829

Make sure that you have nothing after your front matter

---
---
NO NEW LINE HERE !!

No spaces, no new lines

Sometimes text editors will add a newline at the end of the file. You can get rid of that with:

{% assign content = post.content | strip_newlines %}

and then test with:

{% if content == ""  %}

Upvotes: 17

Related Questions