Render lists of items dynamically with {{#each}} blocks. Learn to iterate over arrays, access loop helpers like @index and @first, and generate repetitive HTML without copy-pasting.
The {{#each}} block iterates over an array defined in your data block.
{{#each arrayName}}
<!-- This HTML repeats for each item -->
{{/each}}
For arrays of strings or numbers, use {{ this }} to access the current item.
<script data>
({
fruits: ["Apple", "Banana", "Cherry", "Dragonfruit"]
})
</script>
<ul>
{{#each fruits}}
<li>{{ this }}</li>
{{/each}}
</ul>
Output:
For arrays of objects, access each property by name inside the loop:
<script data>
({
team: [
{ name: "Alice", role: "Designer", avatar: "๐จ" },
{ name: "Bob", role: "Developer", avatar: "๐ป" },
{ name: "Carol", role: "PM", avatar: "๐" }
]
})
</script>
<div class="team-grid">
{{#each team}}
<div class="member">
<span class="avatar">{{ avatar }}</span>
<h3>{{ name }}</h3>
<p>{{ role }}</p>
</div>
{{/each}}
</div>
Output:
Designer
Developer
PM
Inside an {{#each}} block, you get special helper variables:
| Variable | Type | Description | Example (3 items) |
|---|---|---|---|
{{ this }} | any | Current item value (for primitive arrays) | "Apple" |
{{ @index }} | number | 0-based index | 0, 1, 2 |
{{ @number }} | number | 1-based index (human-friendly) | 1, 2, 3 |
{{ @first }} | string | "true" if first item, "" otherwise | "true", "", "" |
{{ @last }} | string | "true" if last item, "" otherwise | "", "", "true" |
{{ @count }} | number | Total number of items | 3 |
<script data>
({
steps: ["Install", "Configure", "Build", "Deploy"]
})
</script>
<ol>
{{#each steps}}
<li>
Step {{ @number }} of {{ @count }}: {{ this }}
{{#if @last}} ๐{{/if}}
</li>
{{/each}}
</ol>
Output:
Pro tip: Use {{ @first }} and {{ @last }} with {{#if}} to add special styling to the first or last item in a list.
This is where loops become really powerful โ generate components dynamically:
<script data>
({
features: [
{ title: "Fast", desc: "Zero runtime JavaScript" },
{ title: "Simple", desc: "Just HTML with superpowers" },
{ title: "Flexible", desc: "Build anything you want" }
]
})
</script>
<div class="features-grid">
{{#each features}}
<use component="card"
title="{{ title }}"
description="{{ desc }}" />
{{/each}}
</div>
Three card components generated from data! Change the array and the page updates automatically.
<script data>
({
navLinks: [
{ label: "Home", href: "/" },
{ label: "About", href: "/about" },
{ label: "Blog", href: "/blog" },
{ label: "Contact", href: "/contact" }
]
})
</script>
<nav>
{{#each navLinks}}
<a href="{{ href }}">{{ label }}</a>
{{/each}}
</nav>
<script data>
({ tags: ["HTML", "CSS", "Static", "Fast", "Modern"] })
</script>
<div class="tags">
{{#each tags}}
<span class="tag">#{{ this }}</span>
{{/each}}
</div>
You should now be able to:
{{#each}} + {{ this }}@index, @number, @first, @last, @count, this