Ins: loops

Back to Ins homepage

prev next

1. simple loop

Loops are implemented via ins-loop attribute or loop attribute inside ins tag. The value of the attribute is array:object where array is the PHP array to loop and object is a state object updated in every iteration with these properties: The object is an associative array, but its members can be also accessed by dot operator (so $item['key'] can be also called as $item.key).
For the following example, we created $data["items"] = ["one","two","three"].
<ul ins-loop="$items:$item">
  <li>$item.order/$item.length: $item
</ul>

Result

2. state object

Here goes another example of creating list with separator
<ins loop="$items:$item">
  $item.value<ins if="!$item.isLast">, </ins>
</ins>

Result

one, two, three

3. nested loop

Loops can be nested and the container elements can be autoclosed adhering to HTML5 standard.
For the following example, we created $data["table"] = [["A","B"],["C","D"]]
<table ins-loop="$table:$row">
  <tr ins-loop="$row:$cell">
    <td>$cell
</table>

Result

A B
C D