巴克励步
发布于:2025-01-02
nil 值。site.pages 变量中,其中某些页面被设置了 category 属性用于指定该页面的内容分类。如果我们利用 map 过滤器将所有页面的 category 属性保存到一个数组中,就会出现如果某个页面没有 category 属性,其在数组中的值就会是 nil。{% assign site_categories = site.pages | map: 'category' %}
{% for category in site_categories %}
{{ category }}
{% endfor %}
business
celebrities
lifestyle
sports
technology
site_categories 数组时,通过使用 compact 过滤器我们可以删除此数组中的所有 nil 值。{% assign site_categories = site.pages | map: 'category' | compact %}
{% for category in site_categories %}
{{ category }}
{% endfor %}
business
celebrities
lifestyle
sports
technology