Liquid简介
Liquid代码可分为对象(Object)、标记(tag)、过滤器(filter)。

对象
Liquid对象包含将动态内容输出到页面的属性。例如,page对象有一个名为title的属性,可用于输出当前页面的标题。若要访问对象属性,可用用(.)操作符,如{ { page.title } }。Liquid对象有三大类型:
- 全局对象。可以在主题的任何文件中使用如
{ { page.title } }。 - 内容对象。用于输出模板和section文件内容如
{ { content_for_layout } }. - 其他对象。只有在特定情况下使用,如可以在产品详情页使用product对象获取产品信息
{ { product.title } }。 Liquid对象的数据类型有以下五种:
| 类型 | 名称 | 描述 |
|---|---|---|
| String | 字符串 | 由单引号或双引号包括的值 |
| Number | 数值 | 浮点数和整数 |
| Boolean | 布尔 | 只能是true或false |
| Nil | 空值 | Liquid当没有可输出的结果时将返回Nil |
| Array | 数组 | 能够存储一组任意类型的变量,无法只通过Liquid语法初始化一个数组,但是可以利用split过滤器将一个字符串分割为一个字符串数组。 |
标记
标记表示Liquid模板语言的逻辑流和控制流。由{% %}标识。它们不产生任何可见的文本,这意味着可以用他们为变量赋值、创建条件和循环逻辑,并且不在页面上最显示出任何逻辑代码。
代码:
{ % assign user = "Liquid" % }
{ % if user % }
Hello { { user } }
{ % endif % }
输出:
Hello Liquid
标记分为三类:
- 控制流。包括if、unless、elif/else以及case/when语句。例 如:
代码:
{ % assign month = 6 % }
{ % case month % }
{ % when 1,2,3 % }
春天到了
{ % when 4,5,6 % }
夏天到了
{ % when 7,8,9 % }
秋天到了
{ % when 10,11,12 % }
冬天到了
{ % else % }
请输入正确月份
{ % endcase % }
输出:
夏天到了
- 迭代。用于重复运行一段代码。 例如:
{ % for product in collection.products % }
{ { product.title } }
{ % endfor % }
可以使用{ % break % }、{ % continue % }标记停止或跳出当前循环。另外for循环可以有多个可选的参数如: - limit:限定循环执行的次数 - offset:从指定索引号开始执行 - range:定义循环执行的范围,可以利用数字或变量来定义执行范围。 - reversed:反转循环的执行顺序。
cycle操作符用来循环一组字符串并按照它们传入的顺序将其输出。每次调用时,传入的参数中的下一个字符串将被输出。cycle必须用在for循环中。cycle的使用场景包括:
- 对表格中的奇数/偶数行输出相应的类。
- 在一行中的最后一列输出一个唯一的类。
输入:
{ %for i in (1..5) % }
{ { i } }{ % cycle '是奇数','是偶数'% }
{ % endfor % }
输出:
1是奇数
2是偶数
3是奇数
4是偶数
5是奇数
- 变量赋值。assign和capture标识符用于创建新的Liquid变量。
代码:
{ % assign favorite_food = 'pizza' % }
{ % assign age = 35 % }
{ % capture about_me % }
I am { { age } } years old and my favorite food is { { favorite_food } }.
{ % endcapture% }
输出:
I am 35 years old and my favorite food is pizza.
可以用increment和decrement创建一个数值变量,这个变量在后续的每次调用时将此变量的值加1或者减1,初始值是0。
输入:
{ % assign var = 10 % }
{ % increment var % }
{ % increment var % }
{ % increment var % }
{ % increment var % }
{ % decrement var % }
{ % decrement var % }
{ { var } }
输出:
0
1
2
3
3
2
10
在上面的例子中,名为”var”的变量是通过assign创建的。increment和decrement标记在相同名称的变量上应用了几次,由此可见increment和decrement标记不会对assign创建的变量及其值产生任何影响。
注意:increment创建的变量初始值为1,decrement创建的变量初始值为-1。
过滤器(Filter)
| 名称 | 描述 | 例子 | 输出 |
|---|---|---|---|
| abs | 返回一个数字的绝对值 | { { -17 |abs } } | 17 |
| append | 将两个字符串拼接起来并返回拼接后的值 | { { “web/post/page” | append:”.html” } } | web/post/page.html |
| at_least | 将数字限制在最小值 | { { 5 |at_least:3} } | 5 |
| at_most | 将数字限制在最大值 | { { 5 |at_most:8 } } | 5 |
| capitalize | 将字符串首字母转为大写 | { { “title”|capitalize } } | Title |
| ceil | 将一个浮点数向上取整并返回一个最接近的整数 | { { 123.45|ceil } } | 124 |
| compact | 删除数组中的所有nil值 | { { site_categories = site.pages | map:’category’|compact } } | |
| concat | 连接多个数组,返回连接后的数组 | { %assign fruits=”apples,oranges,peaches” |split: “,”% }{ % assign vegetables=”carrots,potatos,tomatos” |split:”,” % }{ % assign everything = fruits |concat:vegetables % }{ % for item in everything %} { { item } }{ % endfor % } | apples oranges peaches carrots potatos tomatos |
| date | 将时间戳(timestamp)转换为另一种日期 | { { page.date |date:”\%Y-\%m-\%d \%H:\%M” } } | 2024-04-03 00:00 |
| default | 指定一个默认值 | { { product_price | default: 4.99} } | 4.99 |
| divided_by | 将两个数相除 | { { 18|divided_by:5.0} } | 3 |
| downcase | 用于将字符串中的知个字符转换为小写形式 | { { “APPLE”|downcase } } | apple |
| escape | 对字符串转义操作就是将字符串中的某些字符替换为转义序列 | { { “Have you read’James & the Giant Peach’?” |escape} } | Have you read'James & the Giant Peach'? |
| escape_once | 转义一个字符串并且不修改已经转义过的实体 | { { “1 < 2 & 3” | escape_once } } | 1 < 2 & 3 |
| first | 返回数组中的第一项 | { %assign my_array=”apples,oranges,peaches”|split:”,” % }{ {my_array.first } } | apples |
| floor | 将一个浮点数通过舍弃小数部分得到最近的整数 | { {123.435|floor } } | 123 |
| join | 将数组中的各个字符串合并不能为一个字符串,并将split参数作为字符串之间的分隔符 | { % assign beatles = “John,Paul,George,Ringo” |split:”,”% }{ { beatles |join:”and”} } | JohnandPaulandGeorgeandRingo |
| last | 返回数组中的最后一项 | { %assign my_array=”apples,oranges,peaches”|split:”,” % }{ {my_array.last } } | peaches |
| lstrip | 删除字符串左侧的所有空白符(制表符、空格和换行符) | { { “ So much room for activities! “|lstrip } } | So much room for activities! |
| map | 从对象中提取指定名称的属性的值,并用这些值构建一个数组 | { %assign all_categories = site.pages|map:”category” % }{ %for item in all_categories % }{ { item } }{ %endfor% } | jekyllReactNextliquidweb技术画live生活 |
| minus | 从一个数中减去另一个数 | { { 8|minus:5} } | 3 |
| modulo | 返回除法运算的余数 | { {5|modulo:2} } | 1 |
| newline_to_br | 将所有的换行符(\n)替换成HTML的( )标签 |
{ %capture string_with_newlines % }Hello\n Liquid\n{ % endcapture% }{ { string_with_newlines|newline_to_br} } | Hello\n Liquid\n |
| plus | 两个数相加 | { { 5|plus:4} } | 9 |
| prepend | 在一个字符串前面附加另一个字符串 | { {“apples,oranges,and bananas” |prepend:”Some Fruits: “ } } | Some Fruits: apples,oranges,and bananas |
| remove | 从一个字符串中删除所有出现的另一个子字符串 | { { “I strained to see th train through the rain” |remove:”rain” } } | I sted to see th t through the |
| remove_first | 从一个字符串中仅删除第一次出现的另一个子字符串 | { { “I strained to see th train through the rain” |remove_first:”rain” } } | I sted to see th train through the rain |
| replace | 将参数中给出的第一个参数全部替换为第二个参数 | { { “Take my protein pills and put my helmet on” |replace:”my”,”your”} } | Take your protein pills and put your helmet on |
| replace_first | 将字符串中出现的第一个参数替换为第二个参数 | { { “Take my protein pills and put my helmet on” |replace_first:”my”,”your”} } | Take your protein pills and put my helmet on |
| reverse | 将数组中的所有项的顺序反转。不能操作字符串 | { %assign my_array = “apples,oranges,peaches,plums” |split: “,” % }{ {my_array|reverse|join:”,”} } | plums,peaches,oranges,apples |
| round | 将浮点数舍入到最近的整数或指定的小数位 | { {183.234|round:2 } } | 183.23 |
| rstrip | 将字符串右侧所有的空白字符(制表符、空格符、回车符)删除 | { {“ so much room for activities “|rstrip } } | so much room for activities |
| size | 返回字符串中所包含的字符数或都数组中所包含的条目数量。支持.size标记 | { { “Ground control to Manager Jack” |size} } | 30 |
| slice | 只传入一个参数时将返加此参数下标所对应的单个字符。第二个参数是可选的,用于指定返回的子字符串的长度。如果第一个参数为负数,表示朋字符串的末尾开始计数。 | { {“Liquid”|slice:-3,2} } | ui |
| sort | 对数组中的元素进行排序,排序后的数组按区分大小写的顺序排列 | { %assign my_array=”zebra,octopus,giraffe,Sally Snake”|split:”,” % }{ {my_array |sort|join:”,” } } | Sally Snake,giraffe,octopus,zebra |
| sort_natural | 对数组进行排序,并且大小写无关 | { %assign my_array=”zebra,octopus,giraffe,Sally Snake”|split:”,” % }{ {my_array |sort_natural|join:”,” } } | giraffe,octopus,Sally Snake,zebra |
| split | 根据参数传入的分隔符将字符串分解为数组 | { %assign beatles=”John,Paul,George,Ringo”|split:”,” % }{ %for item in beatles % }{ { item } }{ %endfor% } | JohnPaulGeorgeRingo |
| strip | 删除字符串左右两侧的所有空白字符(制表符、空格、换行符) | { { “ So much room for activities!” |strip} } | So much room for activities! |
| strip_html | 从字符串中删除所有HTML标签 | { { “Have you read Ulysses?” |strip_html} } | Have you read Ulysses? |
| strip_newlines | 从字符串中删除所有换行字符 | { { string_with_newlines | strip_newlines } } | HelloLiquid |
| times | 将一个数乘以另一个数 | { { 4|times:5} } | 20 |
| truncate | 将字符串截短为指定的字符个数,如果指定的字符数量小于字符串的长度,则会在字符串的末尾加一个省略号,并将此省略号计入字符个数中 | { {“Ground control to Manager Jack”|truncate:20 } } | Ground control to… |
| truncatewords | 将字符串截短为指定的单词个数 | { { “Ground control to Manager Jack.”|truncatewords:3} } | Ground control to… |
| uniq | 删除数组中的所有冗余项 | { %assign my_array=”ants,bugs,bees,bugs,ants”|split:”,” % }{ { my_array|uniq|join:”,”} } | ants,bugs,bees |
| upcase | 将字符串中的每个字符都转换为大写形式 | { { “Parker Moore”|upcase} } | PARKER MOORE |
| url_decode | 对于作为URL进行编码或通过url_encode编码的字符串进行解码 | { { “%27Stop%27+said+Fred”|url_decode} } | ‘Stop’ said Fred |
| url_encode | 将字符串中非URL安全的字符转换为百分号编码的字符 | { { “[email protected]” |url_encode} } | john%40liquid.com |