# Expressions

> Reference for OneFuse Jinja2 expressions including literals, math, comparisons, logic, and operators

---

Site [llms.txt](/llms.txt)

---

Expressions get replaced with values when a template is rendered.

Expressions appear inside `{{ ... }}` tags.

## Literals

The simplest form of expressions.

### Boolean

`true` is always true and `false` is always false.

```jinja2
true
false
```

### Dict

A [dict](https://www.w3schools.com/python/python_dictionaries.asp) in Python is a structure that combines keys and values.

Keys must be unique and always have exactly one value.

```jinja2
{
  "foo": "Hello World"
}
```

### Integers

Integers are whole numbers without a decimal part.

The underscore (`_`) character can be used to separate groups for legibility.

```jinja2
42
123_456
```

### Floating point numbers

Floating Point Numbers can be written using a period (`.`) as a decimal mark.

They can be written in scientific notation with an upper or lowercase `e` to indicate the exponent part.

The underscore (`_`) character can be used to separate groups for legibility, but cannot be used in the exponent part.

```jinja2
42.23
42.1e2
123_456.789
```

### List

Everything between two square brackets is a list (`[...]`).

Lists are useful for storing sequential data to be iterated over.

```jinja2
["List", "of", "Objects"]
```

### String

Everything between two double or single quotes is a string.

They are useful whenever you need a string in the template.

```jinja2
"Hello World"
```

### Tuples

Tuples are similar to lists that cannot be modified ("immutable") and usually used to represent items of two or more elements.

If a tuple only has one item, it must be followed by a comma.

```jinja2
("Tuple",)
("Tuple", "of", "values")
```

---

## Math

### Add

Adds two objects together.

Usually the objects are numbers. See the [`~` operator](#other-operators) for string concatenation.

```jinja2
{{ 1 + 1 }}

=> 2
```

### Subtract

Subtracts the second number from the first number.

```jinja2
{{ 3 - 2 }}

=> 1
```

### Divide

#### Divide two numbers

The return value will be a [Floating Point Number](#floating-point-numbers).

```jinja2
{{ 1 / 2 }}

=> 0.5
```

#### Divide two numbers and return the truncated integer result

```jinja2
{{ 20 // 7 }}

=> 2
```

#### Calculate the remainder of an integer division

```jinja2
{{ 11 % 7 }}

=> 4
```

### Multiply

Multiply the left operand with the right one.

This can also be used to repeat a string multiple times.

```jinja2
{{ 2 * 2 }}

=> 4

{{ "!" * 8 }}

=> !!!!!!!!
```

### Raise to the power

Raise the left operand to the power of the right operand.

```jinja2
{{ 2 ** 3 }}

=> 8
```

---

## Comparisons

### Equality (`==`) {#equality}

Compares two objects for equality.

```jinja2
{{ "hello" == "hello" }}

=> true
```

### Inequality (`!=`) {#inequality}

Compares two objects for inequality.

```jinja2
{{ "hello" != "hello" }}

=> false
```

### Greater than (`>`) {#greater-than}

Left hand side is greater than the right hand side.

```jinja2
{{ 5 > 4 }}

=> true
```

### Greater than or equal to (`>=`) {#greater-than-or-equal-to}

Left hand side is greater or equal to the right hand side.

```jinja2
{{ 5 >= 5 }}

=> true
```

### Less than (`<`) {#less-than}

Left hand side is lower than the right hand side.

```jinja2
{{ 5 < 4 }}

=> false
```

### Less than or equal to (`<=`) {#less-than-or-equal-to}

Left hand side is lower or equal to the right hand side.

```jinja2
{{ 5 <= 5 }}

=> true
```

---

## Logic

### `and`

Returns `true` if the left and the right operand are true.

```jinja2
{% set foo = 2 %}
{{ foo > 1 and foo < 5 }}

=> true
```

### `or`

Returns `true` if the left or the right operand are true.

```jinja2
{% set foo = 3 %}
{{ foo < 5 or foo == 3 }}

=> true
```

### `not`

Negates a statement using the [`is`](#is) and [`in`](#in) operators. All other expressions will require a prefix notation.

```jinja2
{{ foo is not bar }}
{{ foo not in bar }}
{{ not (foo and bar) }}
```

### `(expr)`

Group expressions in parentheses.

```jinja2
{% set foo = 2 %}
{{ (foo > 1) and (foo < 5) }}

=> true
```

---

## Other operators

### `in`

Perform a sequence or mapping containment test. Returns `true` if the left operand is contained in the right.

```jinja2
{{ 1 in [1, 2, 3] }}

=> true
```

### `is`

Performs a [Test](/docs/onefuse/templating/tests/).

```jinja2
{{ foo is bar }}
```

### `|`

Applies a [Filter](/docs/onefuse/templating/filters/).

```jinja2
{{ foo | lower }}
```

### `~`

Converts all operands into strings and concatenates them.

```jinja2
{% set foo = "world" %}
{{ "Hello " ~ foo ~ "!" }}

=> Hello world!
```

### `.` or `[]`

Get an attribute of an object.

```jinja2
{{ foo.bar }}
{{ foo["bar"] }}
```

---

## If expression

Learn about using inline `if` expressions.

This is useful in some situations. The general syntax is:

```jinja2
{# <do something> if <something is true> else <do something else> #}
{{ "[{}]".format(page.title) if page.title }}
```

The `else` part is optional. If not provided, the `else` block implicitly evaluates into an **Undefined** object.

Also refer to [Control Structures: if](/docs/onefuse/templating/control-structures/#if).
