treefrog の Otama テンプレート 内 でループする方法を書き留めておきます。
Loop tr Tag
The following code reveals loop of tr
tag.
1 2 3 |
<tr data-tf="@foreach"> <td data-tf="@value"></td> </tr> |
1 2 3 4 5 6 |
@foreach : foreach (QString value, values) { %% } @value ~= value; |
The result is below.
1 2 3 4 5 |
<tr> <td>1</td> <td>2</td> <td>3</td> </tr> |
Loop li Tag
As for li
, the following code will do.
1 2 3 |
<ol> <li data-tf="@foreach"><span data-tf="@value"></span></li> </ol> |
1 2 3 4 5 6 |
@foreach : foreach (QString value, values) %% } @value := value; |
The result is below.
1 2 3 4 5 |
<ol> <li>1</li> <li>2</li> <li>3</li> </ol> |
tr
のときとは違い、 li
内 のspan
タグごと書き換えています。
Create table
Now, let’s create table tag with the above methods. In this time, output row number together.
1 2 3 4 5 6 7 8 |
<table> <tbody> <tr data-tf="@foreach"> <td data-tf="@rowId"></td> <td data-tf="@itemForeach"><span data-tf="@value"></span></td> </tr> </tbody> </table> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#init tfetch(QVector<QVector<float>>, rows); @foreach: int rowId = 0; foreach (QVector<float> values, rows) { %% } @itemForeach: foreach (float f, values) { %% } @rowId ~= ++rowId; @value := f; |
The leftest column shows row number, begin with 0.