不为空的液体过滤器集合
Liquid filter collection where not null
在我 一些 页(不是全部)的前面,我有:
---
top-navigation:
order: 2
---
使用 liquid 我想过滤所有具有 top-navigation
对象的网站页面并按 top-navigation.order
.
排序
我正在尝试 sort:'top-navigation.order'
,但抛出了 undefined method [] for nil:NilClass
的异常。我试过 where:"top-navigation", true
但它不等同于真值。
如何筛选包含 top-navigation
的页面,然后进行排序?
两步:
用包含 top-navigation
键的页面创建一个数组。
我们创建一个空数组,然后推送具有键的项目。
{% assign navposts = ''|split:''%}
{% for post in site.posts %}
{% if post.top-navigation %}
{% assign navposts = navposts|push:post%}
{% endif %}
{% endfor %}
将上面的数组按top-navigation.order
排序
{% assign navposts = navposts|sort: "top-navigation.order"%}
打印结果:
{% for post in navposts %}
<br>{{ post.title }} - {{post.top-navigation}}
{% endfor %}
页面使用 site.pages
.
在 Jekyll 3.2.0+(和 Github 页面)中,您可以像这样使用 where_exp 过滤器:
{% assign posts_with_nav = site.posts | where_exp: "post", "post.top-navigation" %}
在这里,对于 site.posts 中的每个项目,我们将其绑定到 'post' 变量,然后计算表达式 'post.top-navigation'。如果它评估为真,那么它将被选中。
然后,将其与排序放在一起,您将得到:
{%
assign sorted_posts_with_nav = site.posts
| where_exp: "post", "post.top-navigation"
| sort: "top-navigation.order"
%}
Liquid 也有 where
过滤器,当你没有给它一个目标值时,它会选择所有具有该 属性:
真值的元素
{% assign posts_with_nav = site.posts | where: "top-navigation" %}
遗憾的是,此变体不适用于 Jekyll。
在我 一些 页(不是全部)的前面,我有:
---
top-navigation:
order: 2
---
使用 liquid 我想过滤所有具有 top-navigation
对象的网站页面并按 top-navigation.order
.
我正在尝试 sort:'top-navigation.order'
,但抛出了 undefined method [] for nil:NilClass
的异常。我试过 where:"top-navigation", true
但它不等同于真值。
如何筛选包含 top-navigation
的页面,然后进行排序?
两步:
用包含
top-navigation
键的页面创建一个数组。我们创建一个空数组,然后推送具有键的项目。
{% assign navposts = ''|split:''%} {% for post in site.posts %} {% if post.top-navigation %} {% assign navposts = navposts|push:post%} {% endif %} {% endfor %}
将上面的数组按
排序top-navigation.order
{% assign navposts = navposts|sort: "top-navigation.order"%}
打印结果:
{% for post in navposts %}
<br>{{ post.title }} - {{post.top-navigation}}
{% endfor %}
页面使用 site.pages
.
在 Jekyll 3.2.0+(和 Github 页面)中,您可以像这样使用 where_exp 过滤器:
{% assign posts_with_nav = site.posts | where_exp: "post", "post.top-navigation" %}
在这里,对于 site.posts 中的每个项目,我们将其绑定到 'post' 变量,然后计算表达式 'post.top-navigation'。如果它评估为真,那么它将被选中。
然后,将其与排序放在一起,您将得到:
{%
assign sorted_posts_with_nav = site.posts
| where_exp: "post", "post.top-navigation"
| sort: "top-navigation.order"
%}
Liquid 也有 where
过滤器,当你没有给它一个目标值时,它会选择所有具有该 属性:
{% assign posts_with_nav = site.posts | where: "top-navigation" %}
遗憾的是,此变体不适用于 Jekyll。