Liquid 过滤器

所有标准 Liquid 过滤器 都受支持(请参见下方)。

为了简化常见任务,Jekyll 甚至添加了一些方便的过滤器,您可以在此页面上找到所有这些过滤器。您还可以使用 插件 创建自己的过滤器。

说明 过滤器输出

相对 URL

baseurl 配置值添加到输入中,以将 URL 路径转换为相对 URL。建议将其用于托管在域子路径上的网站。

{{ "/assets/style.css" | relative_url }}

/my-baseurl/assets/style.css

绝对 URL

urlbaseurl 值添加到输入中,以将 URL 路径转换为绝对 URL。

{{ "/assets/style.css" | absolute_url }}

http://example.com/my-baseurl/assets/style.css

日期到 XML 架构

将日期转换为 XML 架构(ISO 8601)格式。

{{ site.time | date_to_xmlschema }}

2008-11-07T13:07:54-08:00

日期到 RFC-822 格式

将日期转换为用于 RSS Feed 的 RFC-822 格式。

{{ site.time | date_to_rfc822 }}

Mon, 07 Nov 2008 13:07:54 -0800

日期到字符串

将日期转换为短格式。

{{ site.time | date_to_string }}

07 Nov 2008

日期到美式序数风格的字符串

将日期格式化为美式序数短格式。 3.8.0

{{ site.time | date_to_string: "ordinal", "US" }}

Nov 7th, 2008

日期到长字符串

将日期格式化为长格式。

{{ site.time | date_to_long_string }}

2008 年 11 月 7 日

按英国序数风格将日期转换为长字符串

将日期格式化为序数、英国、长格式。 3.8.0

{{ site.time | date_to_long_string: "ordinal" }}

2008 年 11 月 7 日

其中

选择数组中键具有给定值的所有对象。

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

Where 表达式

选择数组中表达式为真的所有对象。 3.2.0

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

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

{{ site.members | where_exp:"item", "item.projects contains 'foo'" }}

查找

返回数组中第一个对象,该对象的查询属性具有给定值,或者如果数组中没有项目满足给定条件,则返回 nil4.1.0

{{ site.members | find: "graduation_year", "2014" }}

查找表达式

返回数组中第一个对象,该对象的给定表达式计算结果为真,或者如果数组中没有项目满足计算后的表达式,则返回 nil4.1.0

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

{{ site.members | find_exp:"item", "item.graduation_year < 2014" }}

{{ site.members | find_exp:"item", "item.projects contains 'foo'" }}

按组排列

按给定属性对数组的项目进行分组。

{{ site.members | group_by:"graduation_year" }}

[{"name"=>"2013", "items"=>[...]}, {"name"=>"2014", "items"=>[...]}]

按组排列表达式

使用 Liquid 表达式对数组的项目进行分组。 3.4.0

{{ site.members | group_by_exp: "item", "item.graduation_year | truncate: 3, ''" }}

[{"name"=>"201", "items"=>[...]}, {"name"=>"200", "items"=>[...]}]

XML 转义

转义一些文本以在 XML 中使用。

{{ page.content | xml_escape }}

CGI 转义

CGI 转义字符串以用于 URL 中。用适当的 %XX 替换替换所有特殊字符。CGI 转义通常用加号 + 替换空格。

{{ "foo, bar; baz?" | cgi_escape }}

foo%2C+bar%3B+baz%3F

URI 转义

对 URI 中的任何特殊字符进行百分比编码。URI 转义通常用 %20 替换空格。 保留字符 不会被转义。

{{ "http://foo.com/?q=foo, \bar?" | uri_escape }}

http://foo.com/?q=foo,%20%5Cbar?

单词数

统计文本中单词的数量。
v4.1.0 开始,此过滤器采用一个可选参数来控制 input 字符串中中日韩 (CJK) 字符的处理方式。
'cjk' 作为参数传递将把检测到的每个 CJK 字符计为一个单词,而不管它是否由空格分隔。
传递 'auto'(自动检测)的工作方式类似于 'cjk',但如果过滤器用于可能包含或不包含 CJK 字符的变量字符串,则性能更高。

{{ "Hello world!" | number_of_words }}

2

{{ "你好hello世界world" | number_of_words }}

1

{{ "你好hello世界world" | number_of_words: "cjk" }}

6

{{ "你好hello世界world" | number_of_words: "auto" }}

6

数组到句子

将数组转换为句子。对于列出标签很有用。连接符的可选参数。

{{ page.tags | array_to_sentence_string }}

foo, bar, and baz

{{ page.tags | array_to_sentence_string: "or" }}

foo, bar, or baz

Markdown 化

将 Markdown 格式的字符串转换为 HTML。

{{ page.excerpt | markdownify }}

智能化

将“引号”转换为“智能引号”。

{{ page.title | smartify }}

转换 Sass/SCSS

将 Sass 或 SCSS 格式的字符串转换为 CSS。

{{ some_sass | sassify }}

{{ some_scss | scssify }}

Slug 化

将字符串转换为小写 URL “slug”。有关选项,请参见下方。

{{ "The _config.yml file" | slugify }}

the-config-yml-file

{{ "The _config.yml file" | slugify: "pretty" }}

the-_config.yml-file

{{ "The _cönfig.yml file" | slugify: "ascii" }}

the-c-nfig-yml-file

{{ "The cönfig.yml file" | slugify: "latin" }}

the-config-yml-file

数据到 JSON

将哈希或数组转换为 JSON。

{{ site.data.projects | jsonify }}

规范空白

将任何空白字符替换为一个空格。

{{ "a \n b" | normalize_whitespace }}

排序

对数组进行排序。哈希的可选参数 1. 属性名称 2. 空值顺序(firstlast)。

{{ page.tags | sort }}

{{ site.posts | sort: "author" }}

{{ site.pages | sort: "title", "last" }}

示例

从数组中选取一个随机值。或者,选取多个值。

{{ site.pages | sample }}

{{ site.pages | sample: 2 }}

转换为整数

将字符串或布尔值转换为整数。

{{ some_var | to_integer }}

数组过滤器

从数组中推送、弹出、移动和取消移动元素。这些操作是非破坏性的,即它们不会改变数组,而是复制并改变副本。

{{ page.tags | push: "Spokane" }}

["Seattle", "Tacoma", "Spokane"]

{{ page.tags | pop }}

["Seattle"]

{{ page.tags | shift }}

["Tacoma"]

{{ page.tags | unshift: "Olympia" }}

["Olympia", "Seattle", "Tacoma"]

检查

将对象转换为其字符串表示形式以进行调试。

{{ some_var | inspect }}

slugify 过滤器的选项

slugify 过滤器接受一个选项,每个选项指定要过滤的内容。默认值为 default。它们如下(及其过滤内容):

  • none:无字符
  • raw:空格
  • default:空格和非字母数字字符
  • pretty:空格和非字母数字字符,但 ._~!$&'()+,;=@ 除外
  • ascii:空格、非字母数字和非 ASCII 字符
  • latin:与 default 类似,但拉丁字符首先进行音译(例如,àèïòü 转换为 aeiou3.7.0

使用 where 过滤器检测 nil4.0

你可以使用 where 过滤器来检测属性为 nil"" 的文档和页面。例如,

// Using `nil` to select posts that either do not have `my_prop`
// defined or `my_prop` has been set to `nil` explicitly.
{% assign filtered_posts = site.posts | where: 'my_prop', nil %}
// Using Liquid's special literal `empty` or `blank` to select
// posts that have `my_prop` set to an empty value.
{% assign filtered_posts = site.posts | where: 'my_prop', empty %}

where_exp 筛选器中的二元运算符4.0

您可以在传递给 where_exp 筛选器的表达式中使用 Liquid 二元运算符 orand,以便在操作中使用多个条件。

例如,要获取英语恐怖片的文档列表,可以使用以下代码段

{{ site.movies | where_exp: "item", "item.genre == 'horror' and item.language == 'English'" }}

或者要获取基于漫画的电影列表,可以使用以下代码

{{ site.movies | where_exp: "item", "item.sub_genre == 'MCU' or item.sub_genre == 'DCEU'" }}

标准 Liquid 筛选器

为了您的方便,这里列出了所有 Liquid 筛选器,并附有指向官方 Liquid 文档中示例的链接。