Loops and Iteration Examples in Zolt

Basic Loop

Simple iteration over an array:

Loop with Index

Access loop index variables:

  1. Laptop: $999
  2. Mouse: $29
  3. Keyboard: $79

Loop with Index1

1-based indexing:

  1. Laptop
  2. Mouse
  3. Keyboard

Loop Variables

Available automatic variables:

VariableDescription
{$foreach.index}Current index (0-based)
{$foreach.index1}Current index (1-based)
{$foreach.first}true if first element
{$foreach.last}true if last element
{$foreach.even}true if index is even
{$foreach.odd}true if index is odd

First and Last Detection

Mouse

Clearance Item: Keyboard

Even and Odd Styling

Alternate row colors:

Nested Loops

Categories with products:

Technology

Home & Garden

Loop with Condition

Filter products by price:

Loop with Counter

Odd step: Task 1 Even step: Task 2 Odd step: Task 3 Even step: Task 4

Practical Examples

Team Roster

Team Members

User List

User Directory

👤 John (Admin)

Email: john@example.com

👤 Jane

Email: jane@example.com

👤 Bob

Email: bob@example.com

Last updated: 26/02/2026 10:53:46

Current Stock

Widget A

Widget B

⚠️ Low stock! Widget C

FAQ with Categories

General

What is Zolt?

A modern markup language.

Is it free?

Yes, completely open source.

Technical

How do I install?

Run: npm install zolt-cli

What formats?

HTML, PDF, EPUB

Home About Services Contact

Progress Tracker

Sunset

*Photo by John*


Mountain

*Photo by Jane*


Ocean

*Photo by Bob*

Data Table

RankNameScore
1Alice95
2Bob87
3Charlie92

Changelog Generator

Changelog

v2.0.0 (2026-02-01)

v1.9.0 (2026-01-15)

v1.8.0 (2026-01-01)

Card Grid

🎨 Card 1

Description 1

⚡ Card 2

Description 2

🚀 Card 3

Description 3

Complex Examples

Nested Categories and Subcategories

Task Board

To Do
In Progress
Done

Weekly Schedule

Monday

Tuesday

Wednesday

Thursday

Friday

Loop Best Practices

Always Use Descriptive Variable Names

✅ Good:

:::foreach {$products as $product}

❌ Bad:

:::foreach {$items as $i}

Check for Empty Arrays

:::if {List.length($items) > 0}
:::foreach {$items as $item}
- {$item}
:::
:::

Limit Nested Loops

Too many nested loops can be hard to read. Consider restructuring.

Use Conditions Inside Loops

Filter and transform data as needed:

:::foreach {$items as $item}
:::if {$item.active}
- **{$item.name}**
:::
:::
:::

← Back to Examples Index