Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 481
Julia Ebert Avatar asked Dec 05 '14 22:12

Julia Ebert


1 Answers

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 == ""  %}
like image 175
David Jacquel Avatar answered Nov 17 '22 20:11

David Jacquel