3.1 Filters:abs,返回一個數字的絕對值
abs 返回一個數字的絕對值。 輸入 {{ -17 | abs }} 輸出 17 輸入 {{ 4 | abs }} 輸出 4 如果組成字符串的各個字符全是數字,abs?也能夠對此字符串求絕對值。 輸入 {{ "-19.86" | abs }} 輸出 19.86
abs 返回一個數字的絕對值。 輸入 {{ -17 | abs }} 輸出 17 輸入 {{ 4 | abs }} 輸出 4 如果組成字符串的各個字符全是數字,abs?也能夠對此字符串求絕對值。 輸入 {{ "-19.86" | abs }} 輸出 19.86
append 將兩個字符串拼接起來并返回拼接之后的值。 輸入 {{ "/my/fancy/url" | append: ".html" }} 輸出 /my/fancy/url.html append?同樣能夠作用于變量: 輸入 {% assign filename = "/index.html" %} {{ "website.com" | append: filename }} 輸出 website.com/index.html
at_least 將數字限制在最小值。 輸入 {{ 4 | at_least: 5 }} 輸出 5 輸入 {{ 4 | at_least: 3 }} 輸出 4
at_most 將數字限制在最大值。 輸入 {{ 4 | at_most: 5 }} 輸出 4 輸入 {{ 4 | at_most: 3 }} 輸出 3
capitalize 將字符串首字母轉為大寫。 輸入 {{ "title" | capitalize }} 輸出 Title capitalize?只把字符串的首字母轉為大寫,其他字符不受影響: 輸入 {{ "my great title" | capitalize }} 輸出 My great title
ceil 將一個浮點數向上取整并返回一個最接近的整數。在 ceil 過濾器執(zhí)行之前 Liquid 會先嘗試將輸入轉換為數字格式。 輸入 {{ 1.2 | ceil }} 輸出 2 輸入 {{ 2.0 | ceil }} 輸出 2 輸入 {{ 183.357 | ceil }} 輸出 184 以下實例所用輸入是字符串: 輸入 {{ "3.5" | ceil }} 輸出 4
compact 刪除數組中的所有?nil?值。 例如,假定整個網站所有內容頁面作為一個數組保存在?site.pages?變量中,其中某些頁面被設置了?category?屬性用于指定該頁面的內容分類。如果我們利用?map?過濾器將所有頁面的?category?屬性保存到一個數組中,就會出現(xiàn)如果某個頁面沒有?category?屬性,其在數組中的值就會是?nil。 輸入 {...
連接多個數組。生成的數組包含輸入數組中的所有項目。 Input {% assign fruits = "apples, oranges, peaches" | split: ", " %} {% assign vegetables = "carrots, turnips, potatoes" | split: ", " %} {% assign everything = fruits | concat: vegetables %} {% for item in everything %} - {{ item }} {% endfor %} Outp...
date 將時間戳(timestamp)轉換為另一種日期格式。格式化語法與?strftime?一致。輸入格式與 Ruby 中的?Time.parse?一致。 輸入 {{ article.published_at | date: "%a, %b %d, %y" }} 輸出 Fri, Jul 17, 15 輸入 {{ article.published_at | date: "%Y" }} 輸出 2015 date?能夠作用于包含良好格式化的日期字符串: 輸入 {{ "Ma...
default 指定一個默認值,以防預期的值不存在。如果左側的值為?nil、false?或空,default?將輸出此默認值。 如下實例中,product_price?并未被定義,因此將輸出默認值。 輸入 {{ product_price | default: 2.99 }} 輸出 2.99 如下實例中,product_price?已被定義,不再輸出默認值。 輸入 {% assign product_price = 4.99 %} {...
divided_by 將兩個數相除。 如果除數(divisor)為整數,則將相除之后得到的結果向下取整得到最接近的整數(也就是對應?floor?的功能)。 輸入 {{ 16 | divided_by: 4 }} 輸出 4 輸入 {{ 5 | divided_by: 3 }} 輸出 1 控制舍入 divided_by?返回的結果于除數是同一數據類型的,也就是說,如果除數是整數,返回的結果也是整數;...
downcase 用于將字符串中的各個字符轉換為小寫形式。對于已經是小寫形式的字符串沒有任何影響。 輸入 {{ "Parker Moore" | downcase }} 輸出 parker moore 輸入 {{ "apple" | downcase }} 輸出 apple
escape 對字符串轉義操作就是將字符串中的某些字符替換為轉義序列(escape sequence),這樣整個字符串就能夠用于 URL 了。如果字符串不需要轉義則不會對字符串做任何操作。 輸入 {{ "Have you read 'James & the Giant Peach'?" | escape }} 輸出 Have you read 'James & the Giant Peach'? 輸...
escape_once 轉義一個字符串并且不修改已經轉義過的實體(entities)。對于無須轉義的字符串不做任何修改。 輸入 {{ "1 < 2 & 3" | escape_once }} 輸出 1 < 2 & 3 輸入 {{ "1 < 2 & 3" | escape_once }} 輸出 1 < 2 & 3
first 返回數組的第一項。 輸入 {% assign my_array = "apples, oranges, peaches, plums" | split: ", " %} {{ my_array.first }} 輸出 apples 輸入 {% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %} {{ my_array.first }} 輸出 zebra
floor 將一個浮點數通過舍棄小數部分得到最近的整數。在 floor 過濾器執(zhí)行之前 Liquid 會先嘗試將輸入轉換為數字格式。 輸入 {{ 1.2 | floor }} 輸出 1 輸入 {{ 2.0 | floor }} 輸出 2 輸入 {{ 183.357 | floor }} 輸出 183 以下實例所用輸入是字符串: 輸入 {{ "3.5" | floor }} 輸出 3
join 將數組中的各個字符串合并為一個字符串,并將?split?參數作為字符串之間的分隔符。 輸入 {% assign beatles = "John, Paul, George, Ringo" | split: ", " %} {{ beatles | join: " and " }} 輸出 John and Paul and George and Ringo
last 返回數組中的最后一項。 輸入 {% assign my_array = "apples, oranges, peaches, plums" | split: ", " %} {{ my_array.last }} 輸出 plums 輸入 {% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %} {{ my_array.last }} 輸出 tiger
lstrip 刪除字符串左側的所有空白符(制表符、空格和換行符)。字符串中間的所有空白符不受影響。 輸入 {{ " So much room for activities! " | lstrip }} 輸出 So much room for activities!
map 從對象(object)中提取指定名稱的屬性的值,并用這些值構建一個數組。 以下實例中,假定?site.pages?包含了整個網站的元數據信息。利用?assign?和?map?過濾器創(chuàng)建一個變量,此變量只包含?site.pages?對象中?category?屬性對應的所有值。 輸入 {% assign all_categories = site.pages | map: "category" %} {% for item i...
minus 從一個數中減去另一個數。 輸入 {{ 4 | minus: 2 }} 輸出 2 輸入 {{ 16 | minus: 4 }} 輸出 12 輸入 {{ 183.357 | minus: 12 }} 輸出 171.357
modulo 返回除法運算的余數。 輸入 {{ 3 | modulo: 2 }} 輸出 1 輸入 {{ 24 | modulo: 7 }} 輸出 3 輸入 {{ 183.357 | modulo: 12 }} 輸出 3.357
newline_to_br 將所有換行符(n) 替換為 HTML 的 (<br>) 標簽。 輸入 {% capture string_with_newlines %} Hello there {% endcapture %} {{ string_with_newlines | newline_to_br }} 輸出 <br /> Hello<br /> there<br />
plus 兩個數相加。 輸入 {{ 4 | plus: 2 }} 輸出 6 輸入 {{ 16 | plus: 4 }} 輸出 20 輸入 {{ 183.357 | plus: 12 }} 輸出 195.357
prepend 在一個字符串前面附加另一個字符串。 輸入 {{ "apples, oranges, and bananas" | prepend: "Some fruit: " }} 輸出 Some fruit: apples, oranges, and bananas prepend?也能作用于變量: 輸入 {% assign url = "example.com" %} {{ "/index.html" | prepend: url }} 輸出 example.com/index.html
remove 從一個字符串中刪除所有出現(xiàn)的另一個子字符串。 輸入 {{ "I strained to see the train through the rain" | remove: "rain" }} 輸出 I sted to see the t through the
remove_first 從一個字符串中僅僅刪除第一次出現(xiàn)的另一個子字符串。 輸入 {{ "I strained to see the train through the rain" | remove_first: "rain" }} 輸出 I sted to see the 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 將字符串中出現(xiàn)的第一個參數替換為第二個參數。 輸入 {% assign my_string = "Take my protein pills and put my helmet on" %} {{ my_string | replace_first: "my", "your" }} 輸出 Take your protein pills and put my helmet on
reverse 將數組中的所有項的順序反轉。reverse?不能操作字符串。 輸入 {% assign my_array = "apples, oranges, peaches, plums" | split: ", " %} {{ my_array | reverse | join: ", " }} 輸出 plums, peaches, oranges, apples reverse?不能直接應用到字符串上,但是你可以先將字符串分割成字符數組,然后再將數組反轉,最...
round 將浮點數舍入到最近的整數,或者,如果傳入的參數是一個數值的話,將浮點數舍入到參數指定的小數位。 輸入 {{ 1.2 | round }} 輸出 1 輸入 {{ 2.7 | round }} 輸出 3 輸入 {{ 183.357 | round: 2 }} 輸出 183.36
rstrip 將字符串右側的所有空白字符(制表符 - tab、空格符 - space 和 回車符 - newline)刪除。 輸入 {{ " So much room for activities! " | rstrip }} 輸出 So much room for activities!
size 返回字符串中所包含的字符數或者數組中所包含的條目數量。size?還支持“點標記”(例如?{{ my_string.size }})。這種用法便于你在標簽(tag)中使用?size?過濾器,例如條件判斷標簽(tag)。 輸入 {{ "Ground control to Major Tom." | size }} 輸出 28 輸入 {% assign my_array = "apples, oranges, peaches, plums" | s...
slice 只傳入一個參數時將返回此參數作為下標所對應的單個字符。第二個參數是可選的,用于指定返回的子字符串的長度。 String indices are numbered starting from 0. 輸入 {{ "Liquid" | slice: 0 }} 輸出 L 輸入 {{ "Liquid" | slice: 2 }} 輸出 q 輸入 {{ "Liquid" | slice: 2, 5 }} 輸出 quid If the first parameter is ...
sort 對數組中的所有進行排序。排序后的數組是按照區(qū)分大小寫的順序排列的。 輸入 {% 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 根據參數傳入的分隔符將字符串分解為數組。split?通常被用于將以逗號為分隔符的字符串轉換為數組。 輸入 {% assign beatles = "John, Paul, George, Ringo" | split: ", " %} {% for member in beatles %} {{ member }} {% endfor %} 輸出 John Paul George Ringo
strip 刪除字符串左右兩側的所有空白符號(包括制表符、空格、換行符)。對于字符串中間的空白符不做任何處理。 Input {{ " So much room for activities! " | strip }} Output So much room for activities!
strip_html 從字符串中刪除所有 HTML 標簽。 輸入 {{ "Have <em>you</em> read <strong>Ulysses</strong>?" | strip_html }} 輸出 Have you read Ulysses?
strip_newlines 從字符串中刪除所有換行字符(newline character)。 輸入 {% capture string_with_newlines %} Hello there {% endcapture %} {{ string_with_newlines | strip_newlines }} 輸出 Hellothere
times 將一個數乘以另一個數。 輸入 {{ 3 | times: 2 }} 輸出 6 輸入 {{ 24 | times: 7 }} 輸出 168 輸入 {{ 183.357 | times: 12 }} 輸出 2200.284
truncate truncate?將字符串截短為指定的字符個數。如果指定的字符數量小于字符串的長度,則會在字符串末尾添加一個省略號(…) 并將此省略號計入字符個數中。 輸入 {{ "Ground control to Major Tom." | truncate: 20 }} 輸出 Ground control to... 自定義省略號 truncate?還支持第二個可選參數,用于指定一個字符序列,此字符...
truncatewords 將字符串截短為指定的單詞個數。如果指定的單詞數量小于字符串中包含的單詞個數,則會在字符串末尾添加一個省略號(…)。 輸入 {{ "Ground control to Major Tom." | truncatewords: 3 }} 輸出 Ground control to... 自定義省略號 truncatewords?還支持第二個可選參數,用于指定一個字符序列,此字符序列將被添加...
uniq 刪除數組中的所有冗余項。 輸入 {% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " %} {{ my_array | uniq | join: ", " }} 輸出 ants, bugs, bees
upcase 將字符串中的每個字符都轉換為大寫形式。對于已經全是大寫的字符串不做任何操作。 輸入 {{ "Parker Moore" | upcase }} 輸出 PARKER MOORE 輸入 {{ "APPLE" | upcase }} 輸出 APPLE
url_decode 對于作為 URL 進行編碼或通過?url_encode?編碼的字符串進行解碼。 輸入 {{ "%27Stop%21%27+said+Fred" | url_decode }} 輸出 'Stop!' said Fred
url_encode 將字符串中非 URL 安全的字符轉換為百分號編碼(percent-encoded)的字符。 輸入 {{ "john@liquid.com" | url_encode }} 輸出 john%40liquid.com 輸入 {{ "Tetsuro Takara" | url_encode }} 輸出 Tetsuro+Takara
? Copyright 2023 深圳藍曬科技有限公司. 粵ICP備2023054553號-1