Loops & Conditionals

Zolt provides powerful advanced features including loops, conditionals, and built-in charts.

Loops (foreach)

Iterate over arrays to generate dynamic content.

Basic Loop

zolt
$items = ["Apple", "Banana", "Cherry"]

:::foreach {$items as $item}
- {$item}
:::
Result
  • Apple
  • Banana
  • Cherry

Loop with Else

The :::else block is rendered if the collection is empty or doesn't exist.

zolt
$items = []

:::foreach {$items as $item}
- {$item}
:::else
No items found.
:::
Result

No items found.

Loop with Index

zolt
$products = [
  {name: "Laptop", price: 999},
  {name: "Mouse", price: 29},
  {name: "Keyboard", price: 79}
]

:::foreach {$products as $product}
{$foreach.index1}. {$product.name}: ${{$product.price}}
:::
Result
  1. Laptop: $999
  2. Mouse: $29
  3. Keyboard: $79

Loop Variables

Access loop metadata:

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

Examples

zolt
$products = [
  {name: "Laptop", price: 999},
  {name: "Mouse", price: 29},
  {name: "Keyboard", price: 79}
]

:::foreach {$products as $product}
:::if {$foreach.first}
- Featured: {$product.name}
:::elseif {$foreach.last}
- Last Item: {$product.name}
:::else
- {$product.name}
:::
:::
Result
  • Featured: Laptop
  • Mouse
  • Last Item: Keyboard

Conditionals (if)

Display content based on conditions.

Basic Condition

zolt
$enabled = true

:::if {$enabled}
This shows when enabled is true.
:::
Result

This shows when enabled is true.

With Else

zolt
:::if {$enabled}
Content A
:::else
Content B
:::
Result

Content B

With ElseIf

You can chain multiple conditions using :::elseif .

zolt
$status = "warning"

:::if {$status == "success"}
Operation successful!
:::elseif {$status == "warning"}
Proceed with caution.
:::else
An error occurred.
:::
Result

Proceed with caution.

Complex Conditions

zolt
:::if {$count > 10}
Many items!
:::elseif {$count > 0 && $count <= 10}
Few items.
:::else
No items.
:::
Result

Few items.

Logical & Comparison Operators

Zolt supports standard operators for complex conditions and expressions.

Comparison Operators

OperatorDescriptionExample
==Equal to{$count == 10}
!=Not equal to{$role != "admin"}
<Less than{$price < 50}
<=Less than or equal to{$age <= 18}
>Greater than{$count > 0}
>=Greater than or equal to{$score >= 100}

Logical Operators

OperatorAliasDescriptionExample
!-NOT{!$enabled}
&&andAND{$count > 0 && $count < 10}
||orOR{$isAdmin || $isEditor}

Examples

zolt
$count = 5
$enabled = false

:::if {!$enabled && $count > 0}
The system is ready but disabled.
:::
Result

The system is ready but disabled.

Ternary Operator

You can use the ternary operator for quick inline choices: condition ? trueValue : falseValue .

zolt
$isLogged = true

Status: {{ $isLogged ? "Online" : "Offline" }}
Result

Status: Online

Best Practices

  1. Check data before using in loops
  2. Use descriptive variable names
  3. Limit complexity in visualizations
  4. Test conditions thoroughly

© 2026 Marmotz