Liquid基础语法
Output輸出
簡(jiǎn)單輸出示例:
Hello {{name}}
Hello {{user.name}}
Hello {{ 'tobi' }}
Advanced output: Filters 高級(jí)輸出:過(guò)濾器
輸出標(biāo)記需要的過(guò)濾器。過(guò)濾器是簡(jiǎn)單的方法。第一個(gè)參數(shù)在過(guò)濾器的左側(cè)就是過(guò)濾器的輸入,即需要過(guò)濾的內(nèi)容。過(guò)濾器的返回值將是過(guò)濾器運(yùn)行時(shí)過(guò)濾后的左側(cè)的參數(shù)。當(dāng)沒(méi)有更多的過(guò)濾器,模板會(huì)收到結(jié)果字符串。
代碼示例:
Hello {{ 'tobi' | upcase }}
Hello tobi has {{ 'tobi' | size }} letters!
Hello {{ '*tobi*' | textilize | upcase }}
Hello {{ 'now' | date: "%Y %h" }}
Standard Filters標(biāo)準(zhǔn)過(guò)濾器
date -時(shí)間格式化
capitalize-設(shè)置輸入中的某個(gè)單詞*
downcase-將輸入的字符串轉(zhuǎn)換為小寫(xiě)*
upcase-將輸入的字符串轉(zhuǎn)換為大寫(xiě)
first-獲得傳入的數(shù)組的第一個(gè)元素
last-獲得傳入的數(shù)組的最后一個(gè)元素
join-用數(shù)組的分隔符連接數(shù)組中的元素
sort-數(shù)組中的元素排序
map-通過(guò)指定的屬性過(guò)濾數(shù)組中的元素
size-返回一個(gè)數(shù)組或字符串的大小
escape-轉(zhuǎn)義一個(gè)字符串
escape_once-返回HTML的轉(zhuǎn)義版本,而不會(huì)影響現(xiàn)有的實(shí)體轉(zhuǎn)義
strip_html-從字符串去除HTML
strip_newlines -從字符串中去除所有換行符( n)的
newline_to_br-用HTML標(biāo)記替換每個(gè)換行符( n)
replace-替換,例如:{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'
replace_first-替換第一個(gè),例如: '{{barbar' | replace_first:'bar','foo' }} #=> 'foobar'
remove-刪除,例如:{{'foobarfoobar' | remove:'foo' }} #=> 'barbar'
remove_first-刪除第一個(gè),例如:{{ 'barbar' | remove_first:'bar' }} #=> 'bar'
truncate-截取字符串到第x個(gè)字符
truncatewords-截取字符串到第x個(gè)詞
prepend-前置添加字符串,例如:{{ 'bar' | prepend:'foo' }} #=> 'foobar'
append-后置追加字符串,例如:{{'foo' | append:'bar' }} #=> 'foobar'
minus-減法,例如:{{ 4 | minus:2 }} #=> 2
plus-加法,例如:{{'1' | plus:'1' }} #=> '11', {{ 1 | plus:1 }} #=> 2
times-乘法,例如:{{ 5 | times:4 }} #=> 20
divided_by-除法,例如:{{ 10 | divided_by:2 }} #=> 5
split-通過(guò)正則表達(dá)式切分字符串為數(shù)組,例如:{{"a~b" | split:"~" }} #=> ['a','b']
modulo-取模,例如:{{ 3 | modulo:2 }} #=> 1
標(biāo)記
目前支持的標(biāo)記的列表:
assign -將某個(gè)值賦給一個(gè)變量
capture-標(biāo)記文本賦值給一個(gè)變量
case-標(biāo)準(zhǔn)的case...when代碼塊
comment-塊標(biāo)記,注釋掉該塊中的文本
cycle-通常在循環(huán)中使用的值之間交替,如顏色或DOM類。
for-for循環(huán)
if-標(biāo)準(zhǔn)的if/else代碼塊
include -包含另外一個(gè)模版
raw-暫時(shí)停用標(biāo)簽處理以避免出現(xiàn)語(yǔ)法沖突
unless-if的反義詞
Comments
注釋是最簡(jiǎn)單的標(biāo)簽,它會(huì)隱藏標(biāo)記的內(nèi)容。例如:
We made 1 million dollars {% comment %} in losses {% endcomment %} this year.
Raw
Raw暫時(shí)禁用標(biāo)簽處理。這是用于生成內(nèi)容,它使用相互矛盾的語(yǔ)法非常有用。例如:
{% raw %}
In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.
{% endraw %}
If / Else
if / else語(yǔ)句對(duì)任何其他編程語(yǔ)言都應(yīng)該是眾所周知的。Liquid允許使用if,unless,以及可選的elsif和else,例如:
{% if user %}
Hello {{ user.name }}
{% endif %}
# Same as above
{% if user != null %}
Hello {{ user.name }}
{% endif %}
{% if user.name == 'tobi' %}
Hello tobi
{% elsif user.name == 'bob' %}
Hello bob
{% endif %}
{% if user.name == 'tobi' or user.name == 'bob' %}
Hello tobi or bob
{% endif %}
{% if user.name == 'bob' and user.age > 45 %}
Hello old bob
{% endif %}
{% if user.name != 'tobi' %}
Hello non-tobi
{% endif %}
# Same as above
{% unless user.name == 'tobi' %}
Hello non-tobi
{% endunless %}
# Check for the size of an array
{% if user.payments == empty %}
you never paid !
{% endif %}
{% if user.payments.size > 0 %}
you paid !
{% endif %}
{% if user.age > 18 %}
Login here
{% else %}
Sorry, you are too young
{% endif %}
# array = 1,2,3
{% if array contains 2 %}
array includes 2
{% endif %}
# string = 'hello world'
{% if string contains 'hello' %}
string includes 'hello'
{% endif %}
Case Statement
如果您需要更多的條件,您可以使用case語(yǔ)句:
{% case condition %}
{% when 1 %}
hit 1
{% when 2 or 3 %}
hit 2 or 3
{% else %}
... else ...
{% endcase %}
例如:
{% case template %}
{% when 'label' %}
// {{ label.title }}
{% when 'product' %}
// {{ product.vendor | link_to_vendor }} / {{ product.title }}
{% else %}
// {{page_title}}
{% endcase %}
Cycle
通常你有不同的顏色或類似的任務(wù)之間切換。 Liquid已經(jīng)內(nèi)置了對(duì)此類操作的支持,使用cycle標(biāo)記。
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
結(jié)果為:
one two three one
如果未提供循環(huán)體的名稱,那么它假設(shè)用同樣的參數(shù)多次調(diào)用同一個(gè)循環(huán)體。
如果你想完全控制循環(huán)體,您可以選擇指定循環(huán)體的名稱。這甚至可以是一個(gè)變量。
{% cycle 'group 1': 'one', 'two', 'three' %}
{% cycle 'group 1': 'one', 'two', 'three' %}
{% cycle 'group 2': 'one', 'two', 'three' %}
{% cycle 'group 2': 'one', 'two', 'three' %}
得到結(jié)果為:
one two one two
For loops
Liquid 可以使用for遍歷集合。
{% for item in array %}
{{ item }}
{% endfor %}
當(dāng)遍歷一個(gè)鍵值對(duì)集合時(shí),item[0]是key的值,item[1]則是value的值。
{% for item in hash %}
{{ item[0] }}: {{ item[1] }}
{% endfor %}
在每次for循環(huán)中,下面的輔助變量可用于額外的需求:
forloop.length # => 整個(gè)for循環(huán)的長(zhǎng)度 forloop.index # => 當(dāng)前迭代的索引 forloop.index0 # => 當(dāng)前迭代的索引(從0開(kāi)始) forloop.rindex # => 剩余的迭代次數(shù) forloop.rindex0 # => 剩余的迭代次數(shù)(從0開(kāi)始) forloop.first # => 是否是第一次迭代? forloop.last # => 是否是最后一次迭代?
你還可以使用多個(gè)屬性來(lái)過(guò)濾循環(huán)中的內(nèi)容。
limit:int可以限制你有多少個(gè)項(xiàng)目獲得 offset:int可以讓你從第n項(xiàng)開(kāi)始遍歷。
# array = [1,2,3,4,5,6]
{% for item in array limit:2 offset:2 %}
{{ item }}
{% endfor %}
# results in 3,4
倒序循環(huán)
{% for item in collection reversed %}
{{item}}
{% endfor %}
你可以通過(guò)定義一系列的數(shù)字來(lái)代替現(xiàn)有的集合循環(huán)。范圍可以通過(guò)包括文本和變量的數(shù)字來(lái)定義:
# if item.quantity is 4...
{% for i in (1..item.quantity) %}
{{ i }}
{% endfor %}
# results in 1,2,3,4
Variable Assignment
您可以將數(shù)據(jù)存儲(chǔ)在自己的變量,在輸出或其他標(biāo)記隨意使用。創(chuàng)建一個(gè)變量最簡(jiǎn)單的方法是使用assign標(biāo)簽,它有一個(gè)非常簡(jiǎn)單的語(yǔ)法:
{% assign name = 'freestyle' %}
{% for t in collections.tags %}
{% if t == name %}
Freestyle!
{% endif %}
{% endfor %}
這樣做的另一種方法是將分配true / false值的變量:
{% assign freestyle = false %}
{% for t in collections.tags %}
{% if t == 'freestyle' %}
{% assign freestyle = true %}
{% endif %}
{% endfor %}
{% if freestyle %}
Freestyle!
{% endif %}
如果你想將多個(gè)字符串合并成一個(gè)單一的字符串,并將其保存到變量中,你可以使用capture標(biāo)記。這個(gè)標(biāo)簽“捕獲”內(nèi)容無(wú)論它是否已經(jīng)實(shí)現(xiàn),然后分配捕獲的值。而不是只能捕獲屏幕上已經(jīng)存在的內(nèi)容。
{% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %}
<label for="{{ attribute_name }}">Color:</label>
<select id="{{ attribute_name }}" name="attributes[{{ attribute_name }}]">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
總結(jié)
以上是生活随笔為你收集整理的Liquid基础语法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 人人开源之代码生成器(renren-ge
- 下一篇: 蚯蚓白糖的作用与功效 蚯蚓白糖的药用价值