Jekyll 过滤液体/凝胶模板中的阵列

Jekyll 过滤液体/凝胶模板中的阵列,jekyll,liquid,Jekyll,Liquid,大多数液体“过滤器”实际上是函数式编程意义上的“映射”:取一个数组,对每个元素应用一个函数,然后返回转换后的数组。我想改为“过滤”:我只想返回数组中与特定条件匹配的项目。我该怎么做 具体而言,我正在尝试改进此模板: Authors: {% for c in site.data["contributors"] %} {% assign contributor = c[1] %}{% if contributor.role contains "author" %}{{contributor.n

大多数液体“过滤器”实际上是函数式编程意义上的“映射”:取一个数组,对每个元素应用一个函数,然后返回转换后的数组。我想改为“过滤”:我只想返回数组中与特定条件匹配的项目。我该怎么做

具体而言,我正在尝试改进此模板:

Authors: 
{% for c in site.data["contributors"] %}
  {% assign contributor = c[1] %}{% if contributor.role contains "author" %}{{contributor.name.given}} {{contributor.name.family}}{% endif %}{% endfor %}
经过修饰后,看起来像这样:

Authors: 
{% for c in site.data["contributors"] %}
  {% assign contributor = c[1] %}
  {% if contributor.role contains "author" %}
    {{contributor.name.given}} {{contributor.name.family}}
  {% endif %}
{% endfor %}
ianbarber:
  name:
    given: Ian
    family: Barber
  org:
    name: Google
    unit: Developer Relations
  country: UK
  role:
    - engineer
  homepage: http://riskcompletefailure.com
  google: +ianbarber
  twitter: ianbarber
  email: ianbarber@google.com
  description: "Ian is a DRE"

samdutton:
  name:
    given: Sam
    family: Dutton
  org:
    name: Google
    unit: Developer Relations
  country: UK
  role:
    - author
  google: +SamDutton
  email: dutton@google.com
  description: "Sam is a Developer Advocate"
其数据如下所示:

Authors: 
{% for c in site.data["contributors"] %}
  {% assign contributor = c[1] %}
  {% if contributor.role contains "author" %}
    {{contributor.name.given}} {{contributor.name.family}}
  {% endif %}
{% endfor %}
ianbarber:
  name:
    given: Ian
    family: Barber
  org:
    name: Google
    unit: Developer Relations
  country: UK
  role:
    - engineer
  homepage: http://riskcompletefailure.com
  google: +ianbarber
  twitter: ianbarber
  email: ianbarber@google.com
  description: "Ian is a DRE"

samdutton:
  name:
    given: Sam
    family: Dutton
  org:
    name: Google
    unit: Developer Relations
  country: UK
  role:
    - author
  google: +SamDutton
  email: dutton@google.com
  description: "Sam is a Developer Advocate"
(示例取自)

这种方法的问题是,如果当前元素与条件不匹配,就会输出换行符,如中所示


如何修复此问题?

如果要在条件不匹配时删除换行符,请尝试删除
If
for
子句中不需要的换行符:

{% for c in site.data["contributors"] %}{% assign contributor = c[1] %}{% if contributor.role contains "author" %}
  {{contributor.name.given}} {{contributor.name.family}}{% endif %}{% endfor %}
这在源代码中看起来可能不太好,但可能会在输出中去掉换行符


注意:我没有尝试这个,但类似的重新排列帮助我摆脱了不需要的换行符。您甚至可能需要将
{%if…
放在最左边,这样就不会包括缩进。

Jekyll 2.0有一个
过滤器,其中有
过滤器。请参阅。示例-来自文档:

{{ site.members | where:"graduation_year","2014" }}
{{ site.members | where_exp:"item", "item.graduation_year == 2014" }}

不,这不起作用。事实上,这就是它们在代码库中的作用,而我在我的问题中做了一些修饰。我想我应该说添加这个警告。我更改了格式。再试一次。问题是for和if子句中有新行,它们插入到您不需要的地方。它们应该只插入到您想要的地方它们。因此,重新格式化代码应该去掉这些换行符。在我的代码中,我也必须尝试这样做,以避免换行符。每一个换行符和空格都会被插入,即使你认为这只是为了修饰和格式化。我经常用换行符+数字来替换换行符,以查看哪个换行符放在哪里。然后我会删除我不想要的换行符(和数字)在最终来源中。“大多数液体“过滤器”实际上是“地图”+!。讽刺和恼人的是,唯一不属于液体“过滤器”的是一个真正的f#%$ing过滤器!