Liquid是shopify公司开发的模版语言,shopify是一家电商软件开发公司。
liquid doc中介绍了liquid的语法。
Liquid code can be categorized into `objects`, `tags`, and `filters`。
Objects是用{{和}}围起来的liquid代码
Objects tell Liquid where to show content on a page.
Objects and variable names are denoted by double curly braces: {{ and }}.
example:
{{ page.title }}
Tags是用{ %
和% }
包裹起来的liquid代码,做逻辑处理和control flow。
Tags create the logic and control flow for templates.
They are denoted by curly braces and percent signs: {% and %}.
分为Control flow、Iteration、Variable assignments三类。
Control flow有三个:if-else,unless,case/when
{% if customer.name == 'kevin' %}
Hey Kevin!
{% elsif customer.name == 'anonymous' %}
Hey Anonymous!
{% else %}
Hi Stranger!
{% endif %}
{% unless product.title == 'Awesome Shoes' %}
These shoes are not awesome.
{% endunless %}
{% 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 %}
Iteration: for、
{% for i in (1..5) %}
{% if i == 4 %}
{% continue %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
{% for i in (1..5) %}
{% if i == 4 %}
{% break %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array limit:2 %}
{{ item }}
{% endfor %}
Output:
1 2
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array offset:2 %}
{{ item }}
{% endfor %}
Output:
3 4 5 6
{% for i in (3..5) %}
{{ i }}
{% endfor %}
也可以是使用变量:
{% assign num = 4 %}
{% for i in (1..num) %}
{{ i }}
{% endfor %}
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array reversed %}
{{ item }}
{% endfor %}
Output
6 5 4 3 2 1
类似于生成器,每次吐出下一个字符串,但是必须在for中使用
。
Loops through a group of strings and outputs them in the order that they were passed as parameters.
Each time cycle is called, the next string that was passed as a parameter is output.
example:
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
Output
one
two
three
one
生成一个table,必须在table标签中使用
。
<table>
{% tablerow product in collection.products %}
{{ product.title }}
{% endtablerow %}
</table>
Output:
<table>
<tr class="row1">
<td class="col1">
Cool Shirt
</td>
<td class="col2">
Alien Poster
</td>
<td class="col3">
Batman Poster
</td>
</tr>
</table>
{% tablerow product in collection.products cols:2 %}
{{ product.title }}
{% endtablerow %}
Output
<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>
{% assign my_variable = false %}
{% if my_variable != true %}
This statement is valid.
{% endif %}
{% assign foo = "bar" %}
{{ foo }}
{% capture my_variable %}I am being captured.{% endcapture %}
{{ my_variable }}
变量my_variable的值就是capture中字符串,如果其中有变量,变量解析成值。
初始值为0
{% increment my_counter %}
{% increment my_counter %}
{% increment my_counter %}
Output
0
1
2
注意increment中的变量和assign、capture创建的变量是相互独立
的,仔细看下面列子的output:
{% assign var = 10 %}
{% increment var %}
{% increment var %}
{% increment var %}
{{ var }}
Output
0
1
2
10
初始值为-1
{% decrement variable %}
{% decrement variable %}
{% decrement variable %}
Output
-1
-2
-3
和increment类似,decrement创建的变量是独立
于assign、capture创建的变量的。
Filters用于改变输出的值
example
{{ "/my/fancy/url" | append: ".html" }}
Output
/my/fancy/url.html
可以连续使用
{{ "adam!" | capitalize | prepend: "Hello " }}
Output
Hello Adam!
这是liquid提供的filter:
abs
append
capitalize
ceil
compact
date
default
divided_by
downcase
escape
escape_once
first
floor
join
last
lstrip
map
minus
modulo
newline_to_br
plus
prepend
remove
remove_first
replace
replace_first
reverse
round
rstrip
size
slice
sort
sort_natural
split
strip
strip_html
strip_newlines
times
truncate
truncatewords
uniq
upcase
== 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 contains 'Pack' %}
This product's title contains the word Pack.
{% endif %}
也可以判断字符串数组中是否包含字符串:
{% if product.tags contains 'Hello' %}
This product has been tagged with 'Hello'.
{% endif %}
只能用于字符串
只有nil
和false
为逻辑假,其余变量都是逻辑真,0也是真!
All values in Liquid are truthy except nil and false.
{% assign tobi = "Tobi" %}
{% if tobi %}
This condition will always be true.
{% endif %}
空字符串也为真:
{% if settings.fp_heading %}
<h1>{{ settings.fp_heading }}</h1>
{% endif %}
Output
<h1></h1>
单引号或双引号包裹:
{% assign my_string = "Hello World!" %}
支持整型和浮点型:
{% assign my_int = 25 %}
{% assign my_float = 39.756 %}
true/false:
{% assign foo = true %}
{% assign bar = false %}
Nil is a special empty value that is returned when Liquid code has no results. It is not a string with the characters “nil”.
Arrays hold lists of variables of any type.
遍历:
<!-- if site.users = "Tobi", "Laura", "Tetsuro", "Adam" -->
{% for user in site.users %}
{{ user }}
{% endfor %}
索引:
<!-- if site.users = "Tobi", "Laura", "Tetsuro", "Adam" -->
{{ site.users[0] }}
{{ site.users[1] }}
{{ site.users[3] }}
数组不能
主动创建,只能是liquid生成,可以通过split
将字符串转成数组。
{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
{% for member in beatles %}
{{ member }}
{% endfor %}
The most popular versions of Liquid that exist are Liquid, Shopify Liquid, and Jekyll Liquid.
Liquid的变种定义了自己的tag,filter等。
jekyll-templates中列出了扩充的filter。