> ## Documentation Index
> Fetch the complete documentation index at: https://dokumentation.websale.de/llms.txt
> Use this file to discover all available pages before exploring further.

# Loops

> Reference for loops and branches in WEBSALE templates: combine if, elseif, else and foreach to react to conditions and iterate over lists.

With control structures, you can make the output depend on certain conditions. A distinction is made between branches with *if* or *switch* and the *foreach* loop.

## if branch

With an *if* branch, one or more conditions are checked. Depending on which condition first returns *true*, that branch is displayed and the others are ignored. An *if*/*elseif*/*else* branch must always consist of an *if* branch and optionally any number of *elseif* branches as well as optionally a single *else* branch.

A branch is introduced with *if* and closed with */if*. *elseif* and *else* branches, on the other hand, are not explicitly closed.

The following example shows the different forms of address when existing customers are logged in to the shop. The template has a variable *\$gender* defined, which retrieves the corresponding gender from the customer records.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $gender == "male" }}
       Dear Mr. {{= $customer.lastName }},
   {{ elseif $gender == "female" }}
       Dear Ms. {{= $customer.lastName }},
   {{ else }}
       Dear Sir or Madam,
{{ /if }}
```

## switch branch

As an alternative to *if* branches, you can also use *switch*, *case*, and *default*. With *switch*, you introduce the branch and specify which field should be checked. After *case*, you write the different values without *==*, and *default* corresponds to *else*.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ switch $gender }}
   {{ case "male" }}
       Dear Mr. {{= $customer.lastName }},
   {{ case "female" }}
       Dear Ms. {{= $customer.lastName }},
   {{ default }}
       Dear Sir or Madam,
{{ /switch }}
```

## foreach loop

A *foreach* loop is used to repeat (*iterate*) code sections multiple times. The loop is introduced with *foreach* and closed with */foreach*. The enclosed area is executed once for each element of the iterable object. With each pass, the current element of the iterable object is assigned to the so-called loop variable (in the example below *\$product*) without *var*.

In the following example, *\$productList* contains a list with 3 elements. With each pass through the loop, the respective element is output with the HTML tags.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $productList = ["Shirt", "Blouse", "Trousers"] }}
{{ foreach $product in $productList }}
    <div>   
        Product: {{= $product }} 
    </div>
{{ /foreach }}
```

The following HTML source code is generated:

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
<div>
    Product: Shirt 
</div>
<div>
    Product: Blouse 
</div>
<div>
    Product: Trousers 
</div>
```

Iterating with a *foreach* loop is possible with *strings*, *lists*, and *maps*.
