> For the complete documentation index, see [llms.txt](https://sevenval.gitbook.io/flat/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sevenval.gitbook.io/flat/reference/templating/loop.md).

# loop

Input:

```javascript
{
  "list": [
    1,
    2,
    3
  ]
}
```

Template:

```markup
<template>
{
  "numbers": [
    {{loop list}}
      {{ . }}
    {{end}}
  ]
}
</template>
```

Output:

```javascript
{
  "numbers": [
    1,
    2,
    3
  ]
}
```

The `loop` command iterates over arrays. The expression given must therefore return an array for the loop body to be executed. The loop body is invoked once for each element in the array. For each invocation, the context `.` will be set to the current element of the array.

In the above example, we did not explicitly place a comma in the loop body. However, JSON requires a comma to separate array values or pairs in objects. Therefore, the `loop` command *automatically* produces commas to separate its child productions.

You may also use a literal comma at the end of the loop body. This will not result in duplicate commas. However, as a literal comma is produced as part of each iteration, the last production will end with that comma. This may result in invalid JSON if the surrounding array or object ends after the loop.

## Example: loop over non-JSON data

If the data to be used in a `loop` is not a JSON array, the [`array()` function](/flat/reference/functions/array.md) can be used to convert the data:

Input:

```markup
<html>
<body>
<ul id="list" foo="bar">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>
</body>
</html>
```

Template:

```markup
<template>
[
  {{loop array(/html/body/ul/li)}}
    {{.}}
  {{end}}
]
</template>
```

Output:

```javascript
[
  "One",
  "Two",
  "Three"
]
```

Attributes passed to the `array()` function will be converted to elements with the attribute's name in its `name` attribute, and the value of the attribute as its content. This can be used to iterate over lists of attributes:

Template (identical input as above):

```markup
<template>
{
  {{loop array(//ul/@*)}}
    {{ @name }}: {{.}}
  {{end}}
}
</template>
```

Output:

```javascript
{
  "id": "list",
  "foo": "bar"
}
```
