标记: {% %}
控制流
-
{% if %}{% elseif %}{% else %}{% endif %}
{% if customer.name == 'kevin' %} Hey Kevin! {% elsif customer.name == 'anonymous' %} Hey Anonymous! {% else %} Hi Stranger! {% endif %}
-
{% unless %}{% endunless %}
{% unless product.title == 'Awesome Shoes' %} These shoes are not awesome. {% endunless %}
-
{% case %}{% when %}{% else %}{% endcase %}
{% 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 %}
迭代
-
{% for in %}{% endfor %}
{% for item in (1..10) %} {{ item }} {% endfor %}
-
{% break %} / {% continue %}
<!-- 跳过4 结束于8 --> {% for item in (1..10) %} <% if item == 4 %> <% continue %> <% elseif item == 8 %> <% break %> <% endif %> {{ item }} {% endfor %}
-
{% limit %} / {% offset %}
( 以index为准 )<!-- output : 2-8 --> {% for item in (1..10) offset:2 limit:8 %} {{ item }} {% endfor %}
-
{% range %}
( 以index为准 ) 双点符{% for item in (3..5) %} {{ item }} {% endfor %}
-
{% reversed %} ( 反转 )
( 反转 ){% for item in array reversed %} {{ item }} {% endfor %}
-
{% cycle %}
使用场景: 对表格中的奇数/偶数行输出相应的类(class){% cycle 'one', 'two' %}{% cycle 'one', 'two' %}{% cycle 'one', 'two' %}------------------------- output: one two one
-
{% tablerow %} / {% cols %} / {% limit %} / {% offset %}
<!-- 3-5行 2列 --><table> {% tablerow product in collection.products cols:2 limit:5 offset:3 %} {{ product.title }} {% endtablerow %}</table>
-
{% tablerow range %}
<table>{% tablerow i in (1..num) %} {{ i }}{% endtablerow %}</table>
变量赋值
-
<% assign %>
-
<% capture %>
( 开始符与结束符之间的字符串赋给capture的变量 )<!-- assign 赋值 -->{% assign favorite_food = 'pizza' %}{% assign age = 35 %}<!-- capture 赋值 -->{% capture about_me %}I am {{ age }} and my favorite food is {{ favorite_food }}.{% endcapture %}{{ about_me }}----------------------------- output: I am 35 and my favourite food is pizza.
-
<% increment %>
( 递增变量 ,初始值1 ){% assign var = 10 %}{% increment var %}{% increment var %}{{ var }}---------------------- output: 1 2 10
-
<% decrement %>
( 递减变量 ,初始值-1 ){% capture variable %}10{% endcapture %}{% decrement variable %}{% decrement variable %}{{ variable }}----------------------------- output: -1 -2 10
注意: 在 `increment``decrement` 之中创建的变量与通过 `assign` 或 `capture` 创建的变量是互相独立的。
------------恢复内容开始------------
#### 标记: {% %}控制流
-
{% if %}{% elseif %}{% else %}{% endif %}
{% if customer.name == 'kevin' %} Hey Kevin! {% elsif customer.name == 'anonymous' %} Hey Anonymous! {% else %} Hi Stranger! {% endif %}
-
{% unless %}{% endunless %}
{% unless product.title == 'Awesome Shoes' %} These shoes are not awesome. {% endunless %}
-
{% case %}{% when %}{% else %}{% endcase %}
{% 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 %}
迭代
-
{% for in %}{% endfor %}
{% for item in (1..10) %} {{ item }} {% endfor %}
-
{% break %} / {% continue %}
<!-- 跳过4 结束于8 --> {% for item in (1..10) %} <% if item == 4 %> <% continue %> <% elseif item == 8 %> <% break %> <% endif %> {{ item }} {% endfor %}
-
{% limit %} / {% offset %}
( 以index为准 )<!-- output : 2-8 --> {% for item in (1..10) offset:2 limit:8 %} {{ item }} {% endfor %}
-
{% range %}
( 以index为准 ) 双点符{% for item in (3..5) %} {{ item }} {% endfor %}
-
{% reversed %} ( 反转 )
( 反转 ){% for item in array reversed %} {{ item }} {% endfor %}
-
{% cycle %}
使用场景: 对表格中的奇数/偶数行输出相应的类(class){% cycle 'one', 'two' %}{% cycle 'one', 'two' %}{% cycle 'one', 'two' %}------------------------- output: one two one
-
{% tablerow %} / {% cols %} / {% limit %} / {% offset %}
<!-- 3-5行 2列 --><table> {% tablerow product in collection.products cols:2 limit:5 offset:3 %} {{ product.title }} {% endtablerow %}</table>
-
{% tablerow range %}
<table>{% tablerow i in (1..num) %} {{ i }}{% endtablerow %}</table>
变量赋值
-
<% assign %>
-
<% capture %>
( 开始符与结束符之间的字符串赋给capture的变量 )<!-- assign 赋值 -->{% assign favorite_food = 'pizza' %}{% assign age = 35 %}<!-- capture 赋值 -->{% capture about_me %}I am {{ age }} and my favorite food is {{ favorite_food }}.{% endcapture %}{{ about_me }}----------------------------- output: I am 35 and my favourite food is pizza.
-
<% increment %>
( 递增变量 ,初始值1 ){% assign var = 10 %}{% increment var %}{% increment var %}{{ var }}---------------------- output: 1 2 10
-
<% decrement %>
( 递减变量 ,初始值-1 ){% capture variable %}10{% endcapture %}{% decrement variable %}{% decrement variable %}{{ variable }}----------------------------- output: -1 -2 10
注意: 在 `increment``decrement` 之中创建的变量与通过 `assign` 或 `capture` 创建的变量是互相独立的。