Liquid 语法索引

本文只是一个索引,几乎无任何注释。首先,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" }}

可选过滤器列表

逻辑块

注释

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 块

{% 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>
lzxz1234 13 January 2015