> ## 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.

# Operators

> Reference for WEBSALE template operators: access, math, comparison, logical and the in operator used to evaluate values in expressions.

Operators are used in the template language to access values, calculate values, compare content, or combine conditions. They are a fundamental part of many template expressions, for example in outputs, conditions, or when working with lists and maps.

Unlike functions, operators are not written as a call with parentheses but are used directly within an expression. The existing operators reference covers access, mathematical, comparison, logical, and the containment operator `in` in particular.

***

## Basics

### **Notation**

Operators are used directly within an expression. They can occur in an output as well as in a variable assignment or a condition.

To output the result of an expression directly in the template, the output notation is used:

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= <expression> }}
```

An expression can also first be assigned to its own variable to be reused later:

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myVariable = <expression> }}
```

The existing reference pages already show exactly this pattern for function calls, variables, and operator expressions.

### Operators in outputs and conditions

Operators are particularly often used in these situations:

* when accessing values in maps or lists
* in calculations
* in comparisons in `if` conditions
* when linking multiple conditions
* when checking whether a value is contained in a list or map

**Examples:**

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $myProduct.name }}
{{= $myPrice * $quantity }}
{{= $myProductStock > 0 }}
{{= $myPrice > 10 and $myProductStock > 0 }}
{{= "red" in $myAvailableColors }}
```

The operators page already documents `.` and `[]` for access, mathematical operators, comparison operators, logical operators, and `in`.

### Order and parentheses

As soon as multiple operators are combined in an expression, parentheses should be used to make the desired order unambiguous. This not only increases readability but also prevents misunderstandings in more complex conditions.

Example:

`{{= ($myPrice > 0 and $stock > 0) or $myIsBackorderAllowed }}`

Recommendation:

* Simple expressions can be written directly.
* For mixed comparisons and logical connections, parentheses should be used.
* Especially with `and`, `or`, and `not`, a clear structure is important.

***

## List of operators

### Dot operator `.`

The dot operator is used to access an attribute of a map. The existing operators page describes the dot operator as standard access to attributes of an object and also shows that it does not work with list indices.

Usage example\
Typical examples are accessing product data, customer data, or other structured values.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $myMap.key }}
```

Example – output product name

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $myProduct.name }}
```

Example – output shipping country from an order address

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $order.shippingAddress.country }}
```

Note: the dot operator is not suitable for numeric list indices.

Example – does not work with a list

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myList = [4, 3, 2, 1] }}
{{= $myList.2 }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
No output
```

### Index operator `[]`

The index operator is used to access values in lists or maps. With maps, the key is specified in square brackets. With lists, the numeric index is used. According to the existing documentation, a variable can also be used in the index operator.

Usage example\
Useful for dynamic keys or for accessing a specific list element.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $myObject["key"] }}
{{= $myList[0] }}
```

Example – access a field in a map

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $myProduct["price"] }}
```

Example – output the first element of a list

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myList = [4, 3, 2, 1] }}
{{= $myList[0] }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
4
```

Example – use a dynamic key

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $fieldName = "price" }}
{{= $myProduct[$fieldName] }}
```

Note: list indices start at `0`.

### Addition operator `+`

The operator `+` is used to add numeric values. The current functions page additionally documents that `+` can also be used to concatenate strings as well as merge lists. This contradicts the older operators page; this draft therefore follows the newer functions page.

**Usage example**\
Can be used for calculations, but also for composing texts or merging lists.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value1 + value2 }}
```

Example – add two numbers

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= 10 + 1 }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
11
```

Example – compose article number from prefix and ID

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $prefix = "ART-" }}
{{ var $productId = "10452" }}
{{= $prefix + $productId }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
ART-10452
```

Example – insert number into text

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $quantity = 3 }}
{{= "Number of items in the basket: " + str($quantity) }}
```

Example – merge two lists

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $listA = ["red", "green"] }}
{{ var $listB = ["blue"] }}
{{= $listA + $listB | json }}
```

Note: when concatenating texts, non-string values may need to be converted to a string first using `str()`.

### Subtraction operator `-`

The operator `-` subtracts one value from another. The operators page documents `-` as one of the basic mathematical operators.

Usage example\
Useful for price differences, discounts, or remaining quantities.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value1 - value2 }}
```

Example – calculate price reduction

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $regularPrice = 99.95 }}
{{ var $salePrice = 79.95 }}
{{= $regularPrice - $salePrice }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
20
```

Example – calculate remaining stock after deduction

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $stock = 15 }}
{{ var $reserved = 4 }}
{{= $stock - $reserved }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
11
```

### Multiplication operator `*`

The operator `*` multiplies two numeric values. It is part of the mathematical operators documented on the operators page.

Usage example\
Typical for quantity and price calculations.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value1 * value2 }}
```

Example – calculate subtotal

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $unitPrice = 19.99 }}
{{ var $quantity = 3 }}
{{= $unitPrice * $quantity }}
```

Example – calculate packaging units

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $boxes = 4 }}
{{ var $itemsPerBox = 6 }}
{{= $boxes * $itemsPerBox }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
24
```

### Division operator `/`

The operator `/` divides one numeric value by another numeric value. This operator is also documented in the existing math overview.

Usage example\
Helpful for averages, ratios, or conversions.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value1 / value2 }}
```

Example – calculate average price

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $sum = 90 }}
{{ var $count = 3 }}
{{= $sum / $count }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
30
```

Example – calculate a partial quantity

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= 7.5 / 3 }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
2.5
```

Note\
For divisions, you should ensure that the divisor is not `0`.

### Modulo operator `%`

The modulo operator `%` returns the remainder of a division. The operators page expressly names `%` as the modulo operator.

Usage example\
Useful for checking even and odd values or implementing grid logic in lists.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value1 % value2 }}
```

Example – check whether a number is even

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= 8 % 2 == 0 }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
true
```

Example – mark every third element in a list

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $loop.index % 3 == 0 }}
  <div class="teaser-row-break"></div>
{{ /if }}
```

### Equality operator `==`

The operator `==` checks whether two values are equal. According to the existing reference, comparison operators always return a boolean value, i.e., `true` or `false`.

Usage example\
Can be used for status checks, country checks, or template logic.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value1 == value2 }}
```

Example – check country

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $countryCode == "DE" }}
```

Example – compare price with maximum value

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $maxPrice = 10.00 }}
{{ var $price = 10.00 }}
{{= $price == $maxPrice }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
true
```

### Inequality operator `!=`

The operator `!=` checks whether two values are unequal. It also belongs to the comparison operators documented on the operators page.

Usage example\
Useful when a value explicitly must not correspond to a certain state.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value1 != value2 }}
```

Example – only display when a customer is not blocked

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $customer.status != "blocked" }}
```

### Less than `<`

The operator `<` checks whether the left value is less than the right value. According to the current operators page, comparison operators are intended for such value comparisons.

Example – check price limit

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $price < 50 }}
```

### Greater than `>`

The operator `>` checks whether the left value is greater than the right value.

Example – check minimum order value

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $cartTotal > 100 }}
```

### Less than or equal `<=`

The operator `<=` checks whether a value is less than or equal to another value.

Example – check stock

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $score >= 80 }}
```

### Greater than or equal `>=`

The operator `>=` checks whether a value is greater than or equal to another value.

Example – check minimum age or threshold

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $score >= 80 }}
```

### Logical `and`

The operator `and` links two conditions. The result is only `true` if **both** conditions are met. Exactly this behavior is described with examples on the operators page.

Usage example\
Useful when multiple prerequisites must be met at the same time.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= condition1 and condition2 }}
```

Example – only display the product if the price is valid and stock is available

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $price > 0 and $inventory > 0 }}
```

Example – buy button only for active products with stock

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $product.active and $product.stock > 0 }}
  <button>Add to basket</button>
{{ /if }}
```

### Logical `or`

The operator `or` links two conditions. The result is `true` if **at least one** of the conditions is met. According to the existing reference, `or` belongs to the logical operators.

Usage example\
Suitable for alternative approvals or special cases.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $inventory > 0 or $allowPreorder }}
```

Example – display the product if stock is available or pre-order is allowed

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $inventory > 0 or $allowPreorder }}
```

### Logical `not`

The operator `not` reverses the truth value of an expression. `not` is also part of the logical operators documented on the operators page.

Usage example\
Helpful when checking that a condition is **not** met.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= not condition }}
```

Example – only display when the user is not logged in

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if not $isLoggedIn }}
  <a href="/login">Log in now</a>
{{ /if }}
```

Example – display a message when no stock is available

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if not ($inventory > 0) }}
  <p>Currently not available</p>
{{ /if }}
```

### Containment operator `in`

The operator `in` checks whether the left value is contained in the right object. The existing operators page expressly documents `in` as a containment operator and shows `1 in [1, 2, 3]` as an example.

Usage example\
Useful for country lists, allowed values, color selections, or whitelists.

Notation

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= value in collection }}
```

Example – check whether a color is available

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= "red" in ["red", "green", "blue"] }}
```

Output

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
true
```

Example – check country against allowed country list

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $allowedCountries = ["DE", "AT", "CH"] }}
{{= $countryCode in $allowedCountries }}
```
