Output輸出
簡單輸出示例:
Hello {{name}} Hello {{user.name}} Hello {{ 'tobi' }}
Advanced output: Filters 高級輸出:過濾器
輸出標記需要的過濾器。過濾器是簡單的方法。第一個參數在過濾器的左側就是過濾器的輸入,即需要過濾的內容。過濾器的返回值將是過濾器運行時過濾后的左側的參數。當沒有更多的過濾器,模板會收到結果字符串。
代碼示例:
Hello {{ 'tobi' | upcase }} Hello tobi has {{ 'tobi' | size }} letters! Hello {{ '*tobi*' | textilize | upcase }} Hello {{ 'now' | date: "%Y %h" }}
Standard Filters標准過濾器
date
-時間格式化capitalize
-設置輸入中的某個單詞*downcase
-將輸入的字符串轉換為小寫*upcase
-將輸入的字符串轉換為大寫first
-獲得傳入的數組的第一個元素last
-獲得傳入的數組的最后一個元素join
-用數組的分隔符連接數組中的元素sort
-數組中的元素排序map
-通過指定的屬性過濾數組中的元素size
-返回一個數組或字符串的大小escape
-轉義一個字符串escape_once
-返回HTML的轉義版本,而不會影響現有的實體轉義strip_html
-從字符串去除HTMLstrip_newlines
-從字符串中去除所有換行符(\ n)的newline_to_br
-用HTML標記替換每個換行符(\ n)replace
-替換,例如:{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'
replace_first
-替換第一個,例如:'{{barbar' | replace_first:'bar','foo' }} #=> 'foobar'
remove
-刪除,例如:{{'foobarfoobar' | remove:'foo' }} #=> 'barbar'
remove_first
-刪除第一個,例如:{{ 'barbar' | remove_first:'bar' }} #=> 'bar'
truncate
-截取字符串到第x個字符truncatewords
-截取字符串到第x個詞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
-通過正則表達式切分字符串為數組,例如:{{"a~b" | split:"~" }} #=> ['a','b']
modulo
-取模,例如:{{ 3 | modulo:2 }} #=> 1
標記
目前支持的標記的列表:
- assign -將某個值賦給一個變量
- capture-標記文本賦值給一個變量
- case-標准的case...when代碼塊
- comment-塊標記,注釋掉該塊中的文本
- cycle-通常在循環中使用的值之間交替,如顏色或DOM類。
- for-for循環
- if-標准的if/else代碼塊
- include -包含另外一個模版
- raw-暫時停用標簽處理以避免出現語法沖突
- unless-if的反義詞
Comments
注釋是最簡單的標簽,它會隱藏標記的內容。例如:
We made 1 million dollars {% comment %} in losses {% endcomment %} this year.
Raw
Raw暫時禁用標簽處理。這是用於生成內容,它使用相互矛盾的語法非常有用。例如:
{% raw %} In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not. {% endraw %}
If / Else
if / else
語句對任何其他編程語言都應該是眾所周知的。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
語句:
{% 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
通常你有不同的顏色或類似的任務之間切換。 Liquid已經內置了對此類操作的支持,使用cycle
標記。
{% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %}
結果為:
one two three one
如果未提供循環體的名稱,那么它假設用同樣的參數多次調用同一個循環體。
如果你想完全控制循環體,您可以選擇指定循環體的名稱。這甚至可以是一個變量。
{% cycle 'group 1': 'one', 'two', 'three' %} {% cycle 'group 1': 'one', 'two', 'three' %} {% cycle 'group 2': 'one', 'two', 'three' %} {% cycle 'group 2': 'one', 'two', 'three' %}
得到結果為:
one two one two
For loops
Liquid 可以使用for
遍歷集合。
{% for item in array %} {{ item }} {% endfor %}
當遍歷一個鍵值對集合時,item[0]
是key的值,item[1]
則是value的值。
{% for item in hash %} {{ item[0] }}: {{ item[1] }} {% endfor %}
在每次for
循環中,下面的輔助變量可用於額外的需求:
forloop.length # => 整個for循環的長度 forloop.index # => 當前迭代的索引 forloop.index0 # => 當前迭代的索引(從0開始) forloop.rindex # => 剩余的迭代次數 forloop.rindex0 # => 剩余的迭代次數(從0開始) forloop.first # => 是否是第一次迭代? forloop.last # => 是否是最后一次迭代?
你還可以使用多個屬性來過濾循環中的內容。
limit:int
可以限制你有多少個項目獲得 offset:int
可以讓你從第n項開始遍歷。
# array = [1,2,3,4,5,6] {% for item in array limit:2 offset:2 %} {{ item }} {% endfor %} # results in 3,4
倒序循環
{% for item in collection reversed %} {{item}} {% endfor %}
你可以通過定義一系列的數字來代替現有的集合循環。范圍可以通過包括文本和變量的數字來定義:
# if item.quantity is 4... {% for i in (1..item.quantity) %} {{ i }} {% endfor %} # results in 1,2,3,4
Variable Assignment
您可以將數據存儲在自己的變量,在輸出或其他標記隨意使用。創建一個變量最簡單的方法是使用assign
標簽,它有一個非常簡單的語法:
{% 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 %}
如果你想將多個字符串合並成一個單一的字符串,並將其保存到變量中,你可以使用capture
標記。這個標簽“捕獲”內容無論它是否已經實現,然后分配捕獲的值。而不是只能捕獲屏幕上已經存在的內容。
{% 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>