Back to Ins homepage
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).
- value the string value of the item, can be also obtained by diractly calling object
- key the key associated with the value
- index the position of the array (starts with 0)
- order the position of the array (starts with 1)
- length the length of the array
- isFirst true iff iterating the first item
- isLast true iff iterating the last item
For the following example, we created $data["items"] = ["one","two","three"].
<ul ins-loop="$items:$item"> <li>$item.order/$item.length: $item </ul>
Here goes another example of creating list with separator
<ins loop="$items:$item"> $item.value<ins if="!$item.isLast">, </ins> </ins>
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>