简体   繁体   English

杰基尔的过滤器阵列液体

[英]Filter Array Liquid in Jekyll

I have an include which renders my posts in a certain format. 我有一个包含项,以某种格式呈现我的帖子。 I am passing the posts to the include like below: 我将帖子传递给如下所示的include:

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

However, I would like to filter out a certain category before passing it to the include. 但是,我想先过滤掉某个类别,然后再将其传递给包含。 Does anyone have any ideas how I can achieve this? 有谁知道我如何实现这一目标?

You can do it defining a variable containing an array with all the posts not containing a specific category and then passing that to includes, for example to filter posts containing the category jekyll : 您可以定义一个包含数组的变量,该数组的所有帖子均不包含特定类别,然后将其传递给包括,例如过滤包含jekyll类别的帖子:

  1. We create the array {% assign notjekyllposts = "" | split: "" %} 我们创建数组{% assign notjekyllposts = "" | split: "" %} {% assign notjekyllposts = "" | split: "" %}
  2. Loop all posts {% for apost in site.posts %} 循环所有帖子{% for apost in site.posts %}
  3. Filter those that does not contain the jekyll category adding them to the array: 过滤不包含jekyll类别的那些,将它们添加到数组中:
    {% assign notjekyllposts = notjekyllposts | push: apost %}
  4. Pass the variable to the include tag 将变量传递给include标记

Putting it all together: 放在一起:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM