---
title: for
description: "Repeat content for each item in an array."
---

# for

Repeat content for each item in an array.

## Syntax

```knap
{% for item in items %}...{% endfor %}
```

## Usage

- Use the name after `for` to access the current item. Close the block with `endfor`.
- An empty or missing array produces no output.
- Each item is separated by a line break. Leave an extra blank line before `endfor` to separate paragraphs or tables.

## Loop values

- `loop.index` is the current item number, starting at 1. `loop.index0` starts at 0.
- `loop.first` and `loop.last` are true for the first and last items.
- `loop.length` is the total number of items.

## Example

```json title="Data"
{
  "tags": [
    "science fiction",
    "novel"
  ]
}
```

```knap title="Template"
{% for tag in tags %}- #{{ tag | kebab }}{% endfor %}
```

```md title="Output"
- #science-fiction
- #novel
```

## Related

- [`if`](/logic/if). Render content when a condition is true.
- [`set`](/logic/set). Save a value to use later in the template.
