Bagikan melalui


Tag iterasi

Tag iterasi digunakan untuk menjalankan/membuat blok kode berulang kali.

untuk

Mengeksekusi sebuah blok kode berulang kali. Hal ini paling sering digunakan pada item dalam larik atau Kamus.

Dalam blok tag for, objek forloop tersedia.

Code

{% for child_page in page.children %}

<a href={{ child_page.url }}>{{ child_page.title }}</a>

{% endfor %}

Output

<a href=/parent/child1/>Child 1</a>

<a href=/parent/child2/>Child 2</a>

<a href=/parent/child3/>Child 3</a>

Parameter

Parameter for dapat digunakan sendiri atau dalam kombinasi.

limit

Keluar loop setelah sejumlah tertentu item.

Code

{% for child_page in page.children limit:2 %}

<a href={{ child_page.url }}>{{ child_page.title }}</a>

{% endfor %}

Output

<a href=/parent/child1/>Child 1</a>

<a href=/parent/child2/>Child 2</a>

offset

Memulai loop pada indeks yang diberikan.

Code

{% for child_page in page.children offset:1 %}

<a href={{ child_page.url }}>{{ child_page.title }}</a>

{% endfor %}

Output

<a href=/parent/child2/>Child 2</a>

<a href=/parent/child3/>Child 3</a>

range

Mendefinisikan kisaran angka untuk loop.

Code

{% assign n = 4 %}

{% for i in (2..n) %}

{{ i }}

{% endfor %}

{% for i in (10..14) %}

{{ i }}

{% endfor }}

Output

2 3 4

10 11 12 14

reversed

Mengulang melalui loop dalam urutan terbalik, mulai dari item terakhir.

Code

{% for child_page in page.children reversed %}

<a href={{ child_page.url }}>{{ child_page.title }}</a>

{% endfor %}

Output

<a href=/parent/child3/>Child 3</a>

<a href=/parent/child2/>Child 2</a>

<a href=/parent/child1/>Child 1</a>

cycle

Loop melalui grup string dan meng-output mereka dalam urutan mereka dilewatkan sebagai parameter. Setiap siklus waktu dipanggil, string berikutnya yang diteruskan sebagai parameter adalah dihasilkan.

Code

{% for item in items %}

<div class={% cycle 'red', 'green', 'blue' %}> {{ item }} </div>

{% end %}

Output

<div class=red> Item one </div>

<div class=green> Item two </div>

<div class=blue> Item three </div>

<div class=red> Item four </div>

<div class=green> Item five</div>

tablerow

Menghasilkan tabel HTML. Harus dibungkus dalam tag HTML <table> pembukaan dan </table> penutup.

Dalam blok tag tablerow, tablerowloop tersedia.

Code

<table>

{% tablerow child_page in page.children %}

{{ child_page.title }}

{% endtablerow %}

</table>

Output

<table>

<tr class=row1>

<td class=col1>

Child Page 1

</td>

<td class=col2>

Child Page 2

</td>

<td class=col3>

Child Page 3

</td>

<td class=col4>

Child Page 4

</td>

</tr>

</table>

Parameter

Parameter tablerowcan ini dapat digunakan sendiri atau dalam kombinasi.

Output

<table>

<tr class=row1>

<td class=col1>

Child Page 1

</td>

<td class=col2>

Child Page 2

</td>

</tr>

<tr class=row2>

<td class=col3>

Child Page 3

</td>

<td class=col4>

Child Page 4

</td>

</tr>

</table>

Code

<table>

{% tablerow child_page in page.children cols:2 %}

{{ child_page.title }}

{% endtablerow %}

</table>

Menentukan berapa banyak baris yang harus dimiliki tabel yang dihasilkan.

cols

limit

Keluar loop setelah sejumlah tertentu item.

Code

<table>

{% tablerow child_page in page.children limit:2 %}

{{ child_page.title }}

{% endtablerow %}

</table>

Output

<table>

<tr class=row1>

<td class=col1>

Child Page 1

</td>

<td class=col2>

Child Page 2

</td>

</tr>

</table>

offset

Memulai loop pada indeks yang diberikan.

Code

<table>

{% tablerow child_page in page.children offset:2 %}

{{ child_page.title }}

{% endtablerow %}

</table>

Output

<table>

<tr class=row1>

<td class=col1>

Child Page 3

</td>

<td class=col2>

Child Page 4

</td>

</tr>

</table>

range

Mendefinisikan kisaran angka untuk loop.

Code

<table>

{% tablerow i in (1..3) %}

{{ i }}

{% endtablerow %}

</table>

Baca juga