Jekyll: 检查文章内容是否为空

10

我在一个Jekyll项目中有一系列的文章,其中有些文章只有标题,而有些文章有标题和内容。我想针对每种情况对文章进行不同的处理。例如:

{% 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 %}

但是 if 语句实际上并没有捕获空的文章。我根据 这个页面 设定了条件,但是这三种可能性都没有捕获空的文章。对于如何处理这些文章,有什么想法吗?


4
你尝试测试内容而非其他吗?if post.content != nil - Ken Stipek
1个回答

17

确保在正文之前没有任何内容

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

无空格,无换行。

有时文本编辑器会在文件末尾添加一个新行。您可以使用以下方法消除它:

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

然后使用以下测试:

{% if content == ""  %}

太棒了!我从未想过使用“| strip_newlines”过滤器来黑掉“{{ content }}”变量以实现这一点。谢谢! - Adriano Monecchi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接