本文只是一个索引,几乎无任何注释。首先,Liquid 包括以下两种标记:
{{ 双大括号 }}
{% 大括号+百分号 %}
基本样例:
Hello {{name}}
Hello {{user.name}}
Hello {{ 'tobi' }}
Hello {{ 'tobi' | upcase }}
Hello tobi has {{ 'tobi' | size }} letters!
Hello {{ '*tobi*' | textilize | upcase }}
Hello {{ 'now' | date: "%Y %h" }}
date
- 日期格式化capitalize
- 首字母大写downcase
- 转换为小写upcase
- 转换为大写first
- 获取传数组的第一个结点last
- 获取传数组最后一个结点join
- 按指定的间隔连接数组元素sort
- 传入的数组排序map
-size
- 数组或者字符串的长度escape
- 安全输出escape_once
-strip_html
- 删掉 HTML 标签strip_newlines
- 删掉换行符newline_to_br
- 用 <br /> 替换换行符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
- 截取,如 {{ 'foobarfoobar' | truncate: 5, '.' }} #=> 'foob.'
truncatewords
- 按单词截取prepend
- 如 {{ 'bar' | prepend:'foo' }} #=> 'foobar'
append
- 如 {{ 'foo' | append:'bar' }} #=> 'foobar'
slice
- 截取,参数包括位移和长度,如 {{ "hello" | slice: -3, 3 }} #=> llo
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
case when
语块We made 1 million dollars {% comment %} in losses {% endcomment %} this year
{% raw %}
In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.
{% endraw %}
{% 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 template %}
{% when 'label' %}
// {{ label.title }}
{% when 'product' %}
// {{ product.vendor | link_to_vendor }} / {{ product.title }}
{% else %}
// {{page_title}}
{% endcase %}
{% 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 item in array %}
{{ item }}
{% endfor %}
Map 循环:
{% for item in hash %}
{{ item[0] }}: {{ item[1] }}
{% endfor %}
在循环中还有以下变量可用:
forloop.length # => 循环长度
forloop.index # => 当前索引
forloop.index0 # => 基于 0 的当前索引
forloop.rindex # => 剩余元素数
forloop.rindex0 # => 基于 0 的剩余元素数
forloop.first # => 判断当前是不是第一个元素
forloop.last # => 判断当前是不是最后一个元素
你可以控制循环的开始和结束点:
# 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 %}
也可以循环一个数字范围:
# 如果 item.quantity 的值是 4...
{% for i in (1..item.quantity) %}
{{ i }}
{% endfor %}
# results in 1,2,3,4
{% assign name = 'freestyle' %}
{% for t in collections.tags %}{% if t == name %}
<p>Freestyle!</p>
{% endif %}{% endfor %}
如果你想把几个字符串连接起来后赋值给某变量可以这么干:
{% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %}
<label for="{{ attribute_name }}">Color:</label>
<select name="attributes[{{ attribute_name }}]" id="{{ attribute_name }}">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>