博客原文:http://huangyanxiang.com/2017/09/20/jekyll-with-liquid.htmlhtml
Jekyll的模板中可使用Liquid語法進行取值,計算和數據處理,若是僅僅只爲了使用Jekyll撰寫博客文章,你能夠不須要了解liquid, 可是若是你須要定製Jekyll的主題,靈活的控制你的站點,你確實有必要了解一些liquid相關的語法。git
Liquid和大多數模板語言相似,也是比較簡單的。github
官方網址:web
Liquid能夠分爲objects, tags, and filters.api
使用英文雙大括號對objects進行取值。數組
獲取頁面的標題:cookie
{{ page.title }}
標籤,例如:條件控制,循環,都是包裹在{%
和 %}
中的。app
若是user不爲空,則輸出user.name:less
{% if user %} Hello {{ user.name }}! {% endif %}
對於filter, 你能夠理解爲提供了一系列經常使用的方法,而且支持管道操做。ide
對url地址添加.html
後綴:
{{ "/my/fancy/url" | append: ".html" }}
輸出:
/my/fancy/url.html
首字母大寫,而後在前面添加Hello
:
{{ "adam!" | capitalize | prepend: "Hello " }}
輸出:
Hello Adam!
是否是很easy?
Liquid包含以下操做符:
== |
equals |
!= |
does not equal |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
or |
logical or |
and |
logical and |
舉個栗子,你一看就懂得。
{% if product.title == "Awesome Shoes" %} These shoes are awesome! {% endif %} {% if product.type == "Shirt" or product.type == "Shoes" %} This is a shirt or a pair of shoes. {% endif %}
另外還有一個操做符:contains
, 它能夠判斷字符串的包含,還能夠判斷數組中的字符串包含關係(若是數組中是非字符串的,是不能判斷出來的)。
{% if product.title contains 'Pack' %} This product's title contains the word Pack. {% endif %} {% if product.tags contains 'Hello' %} This product has been tagged with 'Hello'. {% endif %}
和大多數語言同樣,Liquid也有經常使用的數據類型。
true
or false
{% assign foo = true %} {% assign bar = false %}
統一都叫數字,不分整型,浮點型了
{% assign my_int = 25 %} {% assign my_float = 39.756 %}
使用單引號或雙引號包裹的字符串。
{% assign my_string = "Hello World!" %}
相似js的undefined.
Liquid中沒法單獨定義出數組,不過你可使用filter處理獲得數組。
數組的取值採用廣泛接受的索引取值方式:
{{ site.users[0] }} {{ site.users[1] }} {{ site.users[3] }}
對於數組比較經常使用的還有遍歷操做:
{% for user in site.users %} {{ user }} {% endfor %}
這兩個不叫數據類型,可是是條件判斷的依據,他們叫:真 和 假。何時是真?何時是假?很簡單,你只須要記着:Nil 和 false 是假,其餘的都是真就對了,包括0,空字符串都是真。
truthy | falsy | |
---|---|---|
true | • | |
false | • | |
nil | • | |
string | • | |
empty string | • | |
0 | • | |
integer | • | |
float | • | |
array | • | |
empty array | • | |
page | • | |
EmptyDrop | • |
Tags你能夠理解爲Liquid的關鍵詞,如條件判斷if, 循環語句for, Tag是 {%
和 %}
包裹起來的。
註釋是不會被Liquid引擎處理的。Liquid的註釋既能夠做用於單行,又能夠做用於多行。
{% comment %} line comment {% endcomment %} {% comment %} block comment {% endcomment %}
直接看例子吧。
{% if product.title == 'Awesome Shoes' %} These shoes are awesome! {% endif %} {% unless product.title == 'Awesome Shoes' %} These shoes are not awesome. {% endunless %} equals {% if product.title != 'Awesome Shoes' %} These shoes are not awesome. {% endif %} {% if customer.name == 'kevin' %} Hey Kevin! {% elsif customer.name == 'anonymous' %} Hey Anonymous! {% else %} Hi Stranger! {% endif %} {% assign handle = 'cake' %} {% case handle %} {% when 'cake' %} This is a cake {% when 'cookie' %} This is a cookie {% else %} This is not a cake nor a cookie {% endcase %}
{% for i in (1..5) %} {% if i == 4 %} {% break %} {% else %} {{ i }} {% endif %} {% endfor %} {% for i in (1..5) %} {% if i == 4 %} {% continue %} {% else %} {{ i }} {% endif %} {% endfor %} limit <!-- if array = [1,2,3,4,5,6] --> {% for item in array limit:2 %} {{ item }} {% endfor %} 1 2 offset <!-- if array = [1,2,3,4,5,6] --> {% for item in array offset:2 %} {{ item }} {% endfor %} 3 4 5 6 range {% for i in (3..5) %} {{ i }} {% endfor %} {% assign num = 4 %} {% for i in (1..num) %} {{ i }} {% endfor %} 3 4 5 1 2 3 4 reversed <!-- if array = [1,2,3,4,5,6] --> {% for item in array reversed %} {{ item }} {% endfor %} 6 5 4 3 2 1
cycle, 週期循環的意思。
{% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %} one two three one
tablerow, 快速構建表格的行。
<table> {% tablerow product in collection.products %} {{ product.title }} {% endtablerow %} </table> <table> <tr class="row1"> <td class="col1"> Cool Shirt </td> <td class="col2"> Alien Poster </td> <td class="col3"> Batman Poster </td> <td class="col4"> Bullseye Shirt </td> <td class="col5"> Another Classic Vinyl </td> <td class="col6"> Awesome Jeans </td> </tr> </table> {% tablerow product in collection.products cols:2 %} {{ product.title }} {% endtablerow %} <table> <tr class="row1"> <td class="col1"> Cool Shirt </td> <td class="col2"> Alien Poster </td> </tr> <tr class="row2"> <td class="col1"> Batman Poster </td> <td class="col2"> Bullseye Shirt </td> </tr> <tr class="row3"> <td class="col1"> Another Classic Vinyl </td> <td class="col2"> Awesome Jeans </td> </tr> </table> {% tablerow product in collection.products cols:2 limit:3 %} {{ product.title }} {% endtablerow %} {% tablerow product in collection.products cols:2 offset:3 %} {{ product.title }} {% endtablerow %} <!--variable number example--> {% assign num = 4 %} <table> {% tablerow i in (1..num) %} {{ i }} {% endtablerow %} </table> <!--literal number example--> <table> {% tablerow i in (3..5) %} {{ i }} {% endtablerow %} </table>
Liquid, 支持變量的定義和計算。
assign {% assign my_variable = false %} {% if my_variable != true %} This statement is valid. {% endif %} {% assign foo = "bar" %} {{ foo }} capture {% capture my_variable %}I am being captured.{% endcapture %} {{ my_variable }} I am being captured. {% assign favorite_food = 'pizza' %} {% assign age = 35 %} {% capture about_me %} I am {{ age }} and my favorite food is {{ favorite_food }}. {% endcapture %} {{ about_me }} I am 35 and my favourite food is pizza. Creates a new number variable, and increases its value by one every time it is called. The initial value is 0. {% increment my_counter %} {% increment my_counter %} {% increment my_counter %} 0 1 2 Variables created through the increment tag are independent from variables created through assign or capture. {% assign var = 10 %} {% increment var %} {% increment var %} {% increment var %} {{ var }} 0 1 2 10 {% decrement variable %} {% decrement variable %} {% decrement variable %} -1 -2 -3
Liquid提供了一些經常使用的過濾器(方法)。
{{ -17 | abs }} 17 {{ "-19.86" | abs }} 19.86
{% assign filename = "/index.html" %} {{ "website.com" | append: filename }} website.com/index.html
首字母大寫。
{{ "title" | capitalize }} Title {{ "my great title" | capitalize }} My great title
{{ article.published_at | date: "%a, %b %d, %y" }} Fri, Jul 17, 15 {{ article.published_at | date: "%Y" }} 2015 {{ "March 14, 2016" | date: "%b %d, %y" }} Mar 14, 16 To get the current time, pass the special word "now" (or "today") to date: This page was last updated at {{ "now" | date: "%Y-%m-%d %H:%M" }}. This page was last updated at 2017-06-29 14:45.
{{ product_price | default: 2.99 }} 2.99 {% assign product_price = 4.99 %} {{ product_price | default: 2.99 }} 4.99 {% assign product_price = "" %} {{ product_price | default: 2.99 }} 2.99
去除句子首部的空白字符。
{{ " So much room for activities! " | lstrip }} So much room for activities!
去除句子尾部的空白字符。
{{ " So much room for activities! " | rstrip }} So much room for activities!
去除首尾的空白字符。
{{ " So much room for activities! " | strip }} So much room for activities!
加,減,乘,除運算。
{{ 16 | divided_by: 4 }} 4 {{ 5 | divided_by: 3 }} 1 {{ 20 | divided_by: 7.0 }} 2.857142857142857 {% assign my_integer = 7 %} {% assign my_float = my_integer | times: 1.0 %} {{ 20 | divided_by: my_float }} 2.857142857142857
{{ "I strained to see the train through the rain" | remove: "rain" }} I sted to see the t through the {{ "I strained to see the train through the rain" | remove_first: "rain" }} I sted to see the train through the rain
{{ "Take my protein pills and put my helmet on" | replace: "my", "your" }} Take your protein pills and put your helmet on {% 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
Returns a substring of 1 character beginning at the index specified by the argument passed in. An optional second argument specifies the length of the substring to be returned.
截取指定長度的字符串,第二個表示長度,第一個表示索引,從0開始,若是爲負數,從後面往前數,從-1開始。
{{ "Liquid" | slice: 0 }} L {{ "Liquid" | slice: 2 }} q {{ "Liquid" | slice: 2, 5 }} quid {{ "Liquid" | slice: -3, 2 }} ui
Divides an input string into an array using the argument as a separator. split is commonly used to convert comma-separated items from a string to an array.
分割字符串組成一個數組。
{% assign beatles = "John, Paul, George, Ringo" | split: ", " %} {% for member in beatles %} {{ member }} {% endfor %} John Paul George Ringo
固然這裏沒有列舉完全部的filter, 這些是相對比較經常使用的,記着這些,若是知足不了需求,再去官方文檔上翻閱翻閱。
博客原文:http://huangyanxiang.com/2017/09/20/jekyll-with-liquid.html