迭代(或循環(huán))標(biāo)記(iteration tag)用于重復(fù)運(yùn)行一段代碼。
重復(fù)運(yùn)行一段代碼。for?循環(huán)中所能夠使用的屬性請(qǐng)參考?forloop (object)。
for
輸入
{% for product in collection.products %} {{ product.title }} {% endfor %}
輸出
hat shirt pants
循環(huán)過程中若干遇到?break?標(biāo)記(tag)即停止循環(huán)。
break
{% for i in (1..5) %} {% if i == 4 %} {% break %} {% else %} {{ i }} {% endif %} {% endfor %}
1 2 3
循環(huán)過程中若遇到?continue?標(biāo)記(tag)則跳出當(dāng)前循環(huán)。
continue
{% for i in (1..5) %} {% if i == 4 %} {% continue %} {% else %} {{ i }} {% endif %} {% endfor %}
1 2 3 5
限定循環(huán)執(zhí)行的次數(shù)。
<!-- if array = [1,2,3,4,5,6] --> {% for item in array limit:2 %} {{ item }} {% endfor?%}
1 2
從指定索引號(hào)開始執(zhí)行循環(huán)。
<!-- if array = [1,2,3,4,5,6] --> {% for item in array offset:2 %} {{ item }} {% endfor?%}
3 4 5 6
定義循環(huán)執(zhí)行的范圍。可利用數(shù)字或變量來定義此執(zhí)行范圍。
{% for i in (3..5)?%} {{ i }} {% endfor?%} {% assign num = 4 %} {% for i in (1..num)?%} {{ i }} {% endfor?%}
3 4 5 1 2 3 4
反轉(zhuǎn)循環(huán)的執(zhí)行順序。注意和?reverse?過濾器(filter)的拼寫是不同的。
reverse
<!-- if array = [1,2,3,4,5,6] --> {% for item in array reversed %} {{ item }} {% endfor?%}
6 5 4 3 2 1
循環(huán)一組字符串并按照它們傳入的順序?qū)⑵漭敵?。每次調(diào)用?cycle?時(shí),傳入的參數(shù)中的下一個(gè)字符串將被輸出。
cycle
cycle?必須用在?for?循環(huán)中。
{% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %} {% cycle 'one', 'two', 'three' %}
one two three one
cycle?的使用場(chǎng)景包括:
cycle?能夠接受一個(gè)叫做?cycle group?的參數(shù),以便滿足你在模版中需要使用多個(gè)?cycle?代碼塊的情況。如果沒有為 cycle group 命名,那么將會(huì)假定帶有相同參數(shù)的 cycle 調(diào)用屬于同一個(gè)組(group)。
cycle group
生成一個(gè) HTML 表格。必須用?<table>?和?</table>?這兩個(gè) HTML 標(biāo)簽將其包裹起來。
<table>
</table>
<table> {% tablerow product in collection.products %} {{ product.title }} {% endtablerow %} </table>
<table> <tr class="row1"> <td class="col1"> Cool Shirt </td> <td class="col2"> Alien Poster </td> <td class="col3"> Batman Poster </td> <td class="col4"> Bullseye Shirt </td> <td class="col5"> Another Classic Vinyl </td> <td class="col6"> Awesome Jeans </td> </tr> </table>
定義表格應(yīng)當(dāng)有多少列。
{% tablerow product in collection.products cols:2 %} {{ product.title }} {% endtablerow %}
<table> <tr class="row1"> <td class="col1"> Cool Shirt </td> <td class="col2"> Alien Poster </td> </tr> <tr class="row2"> <td class="col1"> Batman Poster </td> <td class="col2"> Bullseye Shirt </td> </tr> <tr class="row3"> <td class="col1"> Another Classic Vinyl </td> <td class="col2"> Awesome Jeans </td> </tr> </table>
在執(zhí)行到指定的腳標(biāo)(index)之后退出 tablerow 。
{% tablerow product in collection.products cols:2 limit:3 %} {{ product.title }} {% endtablerow %}
在指定的腳標(biāo)(index)之后開始執(zhí)行 tablerow 。
{% tablerow product in collection.products cols:2 offset:3 %} {{ product.title }} {% endtablerow %}
定義循環(huán)執(zhí)行的范圍??衫脭?shù)字和變量來定義執(zhí)行范圍。
<!--variable number example--> {% assign num = 4 %} <table> {% tablerow i in (1..num)?%} {{ i }} {% endtablerow?%} </table> <!--literal number example--> <table> {% tablerow i in (3..5)?%} {{ i }} {% endtablerow?%} </table>
? Copyright 2023 深圳藍(lán)曬科技有限公司. 粵ICP備2023054553號(hào)-1