在 Jekyll 中过滤数组液体

Filter Array Liquid in Jekyll

我有一个包含以特定格式呈现我的帖子。我将帖子传递给如下所示的包含:

{% include post-list.html posts=site.posts %}

但是,我想在将某个类别传递给包含之前将其过滤掉。有谁知道我如何实现这一点?

您可以定义一个变量,其中包含一个数组,其中包含所有不包含特定类别的帖子,然后将其传递给 includes,例如过滤包含类别 jekyll:

的帖子
  1. 我们创建数组{% assign notjekyllposts = "" | split: "" %}
  2. 循环所有帖子{% for apost in site.posts %}
  3. 过滤那些不包含 jekyll 类别的将它们添加到数组中:
    {% assign notjekyllposts = notjekyllposts | push: apost %}
  4. 将变量传递给 include 标签

综合起来:

{% assign notjekyllposts = "" | split: "" %}

{% for apost in site.posts %}
  {% unless apost.categories contains 'jekyll' %}
    {% assign notjekyllposts = notjekyllposts | push: apost %}
  {% endunless %}
{% endfor %}
{% include post-list.html posts=notjekyllposts %}