---
title: Compute Field
description: Compute read-only values in Sveltia CMS based on other fields.
sidebar:
  label: Compute
---

The Compute field type stores read-only values computed from other fields in the entry. It automatically updates the value when the dependent fields change.

## User Interface

### Editor

The field isn’t shown in the editor; its value is computed from other fields and updated automatically when they change. It can’t be edited, regardless of the [`readonly`](/docs/fields#readonly) option.

### Preview

A read-only display of the computed value.

## Data Type

A string representing the computed value.

If the [`value_type`](#value-type) option is set to `int` or `float`, the value is a number. If it’s set to `int/string` or `float/string`, the value is the number stored as a string. If the value can’t be computed, for example because a referenced field is empty, the value is `null`, or an empty string for the string types, just like an empty [Number field](/docs/fields/number).

If the template consists of the `{{index}}` variable alone, the value will be a number representing the current index of the item in the list. Combined with other text or variables, it’s part of a string.

If the template contains a UUID variable, the generated UUID is preserved in the saved value across edits.

## Data Validation

No specific data validation is applied to the Compute field, as its value is derived from other fields.

## Options

In addition to the [common field options](/docs/fields#common-options), the Compute field supports the following options:

### Required Options

#### `widget`

- **Type**: `string`
- **Default**: `string`

Must be set to `compute`.

#### `value`

- **Type**: `string`

The value can be computed using a template string that references other fields. If the [`value_type`](#value-type) option is set to a number type, it’s an [arithmetic expression](#arithmetic-expressions) instead. A template supports:

- A value template that defines how to compute the field’s value. It can include references to other fields using the syntax `{{fields.name}}`, where `name` is the name of the field to reference. [String transformations](/docs/string-transformations) can be applied.
  - A Number field is referenced by its stored value, e.g. `1234.5`, rather than its localized display value, e.g. `1,234.5`. String transformations are also applied to the stored value of the referenced field, so a `ternary` transformation, for example, works on a Boolean field’s `true` or `false`.
  - In a Compute field defined in a [rich text editor component](/docs/api/editor-components), a reference like `{{fields.title}}` refers to the component’s own field of that name. If the component has no such field, it refers to the entry’s field instead.
  - In a Compute field within a [List field](/docs/fields/list), a `*` in a reference stands for the current item. For example, `{{fields.lines.*.name}}` in a field of the `lines` list refers to the `name` field of the same item. This also works for nested lists, like `{{fields.orders.*.lines.*.name}}`. A `*` for a list the Compute field isn’t in resolves to nothing, and is reported as a configuration warning.
- The special variable `{{index}}` to reference the current index when used within a list. It only works inside a [List field](/docs/fields/list), including in an [Object field](/docs/fields/object) of a list item. In a nested list, it’s the index of the innermost item.
- The special variables `{{uuid}}`, `{{uuid_short}}` and `{{uuid_shorter}}` to generate a random UUID or its shorter version, just like the [slug template tags](/docs/collections/entries/slugs#slug-template-tags). The UUID is generated once when the entry is created and kept afterwards, even as the other parts of the value change, so it can serve as a stable identifier. A duplicated entry gets a new UUID. See the [example below](#generating-a-unique-identifier).
- A template tag escaped with a backslash, e.g. `\{{ page.url }}`, which is output as is without the backslash. See [Escaping Template Tags](/docs/string-transformations#escaping-template-tags) for details.

:::warning

To be kept, a UUID variable has to be at either end of the template, or only be separated from the ends by literal text and other UUID variables, like `{{fields.slug}}-{{uuid_short}}`.

A UUID variable sitting between two field references, such as `{{fields.a}}{{uuid}}{{fields.b}}`, can’t be told apart from the values around it once saved. A new UUID is therefore generated whenever the entry is opened. The same applies when a string transformation changes the shape of the UUID, e.g. `truncate`; `upper` and `lower` are fine.

:::

### Optional Options

#### `value_type` [#value-type]

- **Type**: `string`
- **Default**: `string`

The type of value to store. Can be one of the following:

- `string`: The [`value`](#value) option is a template, and the result is stored as a string.
- `int`: The `value` option is an [arithmetic expression](#arithmetic-expressions), and the result is rounded to the nearest integer and stored as a number.
- `float`: The `value` option is an arithmetic expression, and the result is stored as a floating-point number.
- `int/string`: Same as `int`, but the result is stored as a string.
- `float/string`: Same as `float`, but the result is stored as a string.

## Arithmetic Expressions

If the [`value_type`](#value-type) option is set to anything other than `string`, the `value` option is an arithmetic expression that computes a number from other fields, like `{{fields.price}} * {{fields.quantity}}`. An expression can contain:

- Numbers, like `100` or `0.5`.
- References to other fields using the syntax `{{fields.name}}`, as well as the `{{index}}` variable in a [List field](/docs/fields/list). [String transformations](/docs/string-transformations) can be applied, so `{{fields.discount | default('0')}}` treats an empty field as zero. The UUID variables can’t be used.
  - A [Select](/docs/fields/select) or [Relation](/docs/fields/relation) field is referenced by its stored value, e.g. `15`, rather than the label of its option, so an option value can be used in a calculation. With the `multiple` option, `{{fields.extras.*}}` covers each selected value, e.g. `sum({{fields.extras.*}})`.
- The `+` (addition), `-` (subtraction or negation), `*` (multiplication), `/` (division) and `%` (remainder) operators. Multiplication, division and remainder are evaluated before addition and subtraction.
- Parentheses to group operations, like `({{fields.a}} + {{fields.b}}) / 2`.
- The following functions:
  - `round(x)` rounds `x` to the nearest integer, and `round(x, digits)` to the given number of decimal places, e.g. `round({{fields.price}} * 1.13, 2)`. Halves are rounded away from zero, so `round(2.5)` is `3` and `round(-2.5)` is `-3`. The result has no floating-point error: `round(1.005, 2)` is `1.01`.
  - `floor(x)` and `ceil(x)` round `x` down or up to an integer.
  - `abs(x)` returns the absolute value of `x`.
- The following aggregate functions, which take any number of values, including every item of a list:
  - `sum(x, y, …)` returns the sum of the values, or `0` if there are none.
  - `avg(x, y, …)` returns the average of the values.
  - `min(x, y, …)` and `max(x, y, …)` return the smallest or largest of the values.
  - `count(x, y, …)` returns the number of values, which don’t have to be numbers, e.g. `count({{fields.lines.*.name}})`.

### Aggregating List Items

A field reference passed directly to an aggregate function can cover every item of a list. The last `*` in it stands for every item rather than the current one. So `sum({{fields.lines.*.amount}})` adds up the `amount` of every item in the `lines` list, even in a field of a `lines` item.

Any other `*` still stands for the current item if the Compute field is in that list. So `sum({{fields.orders.*.lines.*.amount}})` in a field of an `orders` item adds up the lines of that order only.

Empty values are left out, so an item that has yet to be filled in doesn’t empty the result. However, `avg`, `min` and `max` leave the result empty if there is no value at all.

### Number Conversion

Each field reference is used as a number:

- the stored value of a Number field, or of another Compute field that yields a number
- the text of any other field, which must be a number like `12.5`

The value is left empty if a referenced field is empty or doesn’t hold a number, or if the result isn’t a finite number, as with a division by zero.

Small floating-point errors are removed from the result, so `0.1 * 3` is stored as `0.3` rather than `0.30000000000000004`.

### Errors and Circular References

An invalid expression or a UUID variable in it is reported as a configuration error.

A Compute field can’t refer to itself, as in `sum({{fields.lines.*.amount}})` for the `amount` field of the `lines` list, because its value would change every time it’s computed. For the same reason, Compute fields can’t refer to each other in a cycle, like `a` referring to `b` and `b` referring back to `a`, directly or through other Compute fields. Such references are reported as configuration warnings.

## Examples

### Basic Example

This example demonstrates a Compute field that concatenates the values of two string fields, `first_name` and `last_name`, to create a `full_name` field.

```yaml YAML
fields:
  - name: first_name
    label: First Name
    widget: string
  - name: last_name
    label: Last Name
    widget: string
  - name: full_name
    label: Full Name
    widget: compute
    value: '{{fields.first_name}} {{fields.last_name}}'
```

```toml TOML
[[fields]]
name = "first_name"
label = "First Name"
widget = "string"
[[fields]]
name = "last_name"
label = "Last Name"
widget = "string"
[[fields]]
name = "full_name"
label = "Full Name"
widget = "compute"
value = "{{fields.first_name}} {{fields.last_name}}"
```

```json JSON
{
  "fields": [
    {
      "name": "first_name",
      "label": "First Name",
      "widget": "string"
    },
    {
      "name": "last_name",
      "label": "Last Name",
      "widget": "string"
    },
    {
      "name": "full_name",
      "label": "Full Name",
      "widget": "compute",
      "value": "{{fields.first_name}} {{fields.last_name}}"
    }
  ]
}
```

```js JavaScript
{
  fields: [
    {
      name: 'first_name',
      label: 'First Name',
      widget: 'string',
    },
    {
      name: 'last_name',
      label: 'Last Name',
      widget: 'string',
    },
    {
      name: 'full_name',
      label: 'Full Name',
      widget: 'compute',
      value: '{{fields.first_name}} {{fields.last_name}}',
    },
  ],
}
```

Output example when `first_name` is “John” and `last_name` is “Doe”:

```yaml YAML
first_name: John
last_name: Doe
full_name: John Doe
```

```toml TOML
first_name = "John"
last_name = "Doe"
full_name = "John Doe"
```

```json JSON
{
  "first_name": "John",
  "last_name": "Doe",
  "full_name": "John Doe"
}
```

### Email Link

This example demonstrates a Compute field that generates a mailto link using an email address from another String field.

```yaml YAML
fields:
  - name: contact_email
    label: Contact Email
    widget: string
    type: email
  - name: contact_email_link
    label: Contact Email Link
    widget: compute
    value: 'mailto:{{fields.contact_email}}?subject=Inquiry'
```

```toml TOML
[[fields]]
name = "contact_email"
label = "Contact Email"
widget = "string"
type = "email"
[[fields]]
name = "contact_email_link"
label = "Contact Email Link"
widget = "compute"
value = "mailto:{{fields.contact_email}}?subject=Inquiry"
```

```json JSON
{
  "fields": [
    {
      "name": "contact_email",
      "label": "Contact Email",
      "widget": "string",
      "type": "email"
    },
    {
      "name": "contact_email_link",
      "label": "Contact Email Link",
      "widget": "compute",
      "value": "mailto:{{fields.contact_email}}?subject=Inquiry"
    }
  ]
}
```

```js JavaScript
{
  fields: [
    {
      name: 'contact_email',
      label: 'Contact Email',
      widget: 'string',
      type: 'email',
    },
    {
      name: 'contact_email_link',
      label: 'Contact Email Link',
      widget: 'compute',
      value: 'mailto:{{fields.contact_email}}?subject=Inquiry',
    },
  ],
}
```

Output example when `contact_email` is “contact@example.com”:

```yaml YAML
contact_email: contact@example.com
contact_email_link: mailto:contact@example.com?subject=Inquiry
```

```toml TOML
contact_email = "contact@example.com"
contact_email_link = "mailto:contact@example.com?subject=Inquiry"
```

```json JSON
{
  "contact_email": "contact@example.com",
  "contact_email_link": "mailto:contact@example.com?subject=Inquiry"
}
```

### Generating a Description

This example generates a `description` from the beginning of a [RichText field](/docs/fields/richtext) named `body`. The [`plainify`](/docs/string-transformations#plainify) transformation removes the Markdown syntax and HTML tags, and [`truncate`](/docs/string-transformations#truncate) then shortens the result to 50 characters.

```yaml YAML
fields:
  - name: body
    label: Body
    widget: richtext
  - name: description
    label: Description
    widget: compute
    value: "{{fields.body | plainify | truncate(50, '...')}}"
```

```toml TOML
[[fields]]
name = "body"
label = "Body"
widget = "richtext"
[[fields]]
name = "description"
label = "Description"
widget = "compute"
value = "{{fields.body | plainify | truncate(50, '...')}}"
```

```json JSON
{
  "fields": [
    {
      "name": "body",
      "label": "Body",
      "widget": "richtext"
    },
    {
      "name": "description",
      "label": "Description",
      "widget": "compute",
      "value": "{{fields.body | plainify | truncate(50, '...')}}"
    }
  ]
}
```

```js JavaScript
{
  fields: [
    {
      name: 'body',
      label: 'Body',
      widget: 'richtext',
    },
    {
      name: 'description',
      label: 'Description',
      widget: 'compute',
      value: "{{fields.body | plainify | truncate(50, '...')}}",
    },
  ],
}
```

Output example when `body` has a bold word and a link:

```yaml YAML
body: Let’s **welcome** our [guest](https://example.com) for a chat about the new season of the show.
description: Let’s welcome our guest for a chat about the new s...
```

```toml TOML
body = "Let’s **welcome** our [guest](https://example.com) for a chat about the new season of the show."
description = "Let’s welcome our guest for a chat about the new s..."
```

```json JSON
{
  "body": "Let’s **welcome** our [guest](https://example.com) for a chat about the new season of the show.",
  "description": "Let’s welcome our guest for a chat about the new s..."
}
```

### Using `index` in a List

The `{{index}}` variable can be used within a list to reference the current item’s index. In this example, we create a list of items where each item has a computed `index` based on its index in the list.

```yaml YAML
fields:
  - name: items
    label: Items
    widget: list
    fields:
      - name: name
        label: Name
        widget: string
      - name: index
        label: Item Index
        widget: compute
        value: '{{index}}'
```

```toml TOML
[[fields]]
name = "items"
label = "Items"
widget = "list"

[[fields.fields]]
name = "name"
label = "Name"
widget = "string"

[[fields.fields]]
name = "index"
label = "Item Index"
widget = "compute"
value = "{{index}}"
```

```json JSON
{
  "fields": [
    {
      "name": "items",
      "label": "Items",
      "widget": "list",
      "fields": [
        {
          "name": "name",
          "label": "Name",
          "widget": "string"
        },
        {
          "name": "index",
          "label": "Item Index",
          "widget": "compute",
          "value": "{{index}}"
        }
      ]
    }
  ]
}
```

```js JavaScript
{
  fields: [
    {
      name: 'items',
      label: 'Items',
      widget: 'list',
      fields: [
        {
          name: 'name',
          label: 'Name',
          widget: 'string',
        },
        {
          name: 'index',
          label: 'Item Index',
          widget: 'compute',
          value: '{{index}}',
        },
      ],
    },
  ],
}
```

Output example when three items are added with names “Apple”, “Banana” and “Cherry”:

```yaml YAML
items:
  - name: Apple
    index: 0
  - name: Banana
    index: 1
  - name: Cherry
    index: 2
```

```toml TOML
[[items]]
name = "Apple"
index = 0
[[items]]
name = "Banana"
index = 1
[[items]]
name = "Cherry"
index = 2
```

```json JSON
{
  "items": [
    {
      "name": "Apple",
      "index": 0
    },
    {
      "name": "Banana",
      "index": 1
    },
    {
      "name": "Cherry",
      "index": 2
    }
  ]
}
```

### Generating a Unique Identifier

The `{{uuid}}`, `{{uuid_short}}` and `{{uuid_shorter}}` variables can be combined with other fields to build a readable identifier that stays unique. In this example, the `id` field combines the slugified title with a short random string. Once the entry has been saved, the random part is kept even if the title is edited later.

```yaml YAML
fields:
  - name: title
    label: Title
    widget: string
  - name: id
    label: ID
    widget: compute
    value: '{{fields.title | slugify}}-{{uuid_shorter}}'
```

```toml TOML
[[fields]]
name = "title"
label = "Title"
widget = "string"
[[fields]]
name = "id"
label = "ID"
widget = "compute"
value = "{{fields.title | slugify}}-{{uuid_shorter}}"
```

```json JSON
{
  "fields": [
    {
      "name": "title",
      "label": "Title",
      "widget": "string"
    },
    {
      "name": "id",
      "label": "ID",
      "widget": "compute",
      "value": "{{fields.title | slugify}}-{{uuid_shorter}}"
    }
  ]
}
```

```js JavaScript
{
  fields: [
    {
      name: 'title',
      label: 'Title',
      widget: 'string',
    },
    {
      name: 'id',
      label: 'ID',
      widget: 'compute',
      value: '{{fields.title | slugify}}-{{uuid_shorter}}',
    },
  ],
}
```

Output example when `title` is “Hello World”:

```yaml YAML
title: Hello World
id: hello-world-e7bc7d91
```

```toml TOML
title = "Hello World"
id = "hello-world-e7bc7d91"
```

```json JSON
{
  "title": "Hello World",
  "id": "hello-world-e7bc7d91"
}
```

### Calculating a Total

The `value_type` option turns the `value` option into an arithmetic expression. In this example, each item of the `lines` list has an `amount` field that multiplies the item’s price by its quantity, and a `line` field that numbers the items from 1 rather than 0. The `total` field adds up the amounts and applies the discount, which is treated as zero when the field is left empty.

```yaml YAML
fields:
  - name: lines
    label: Lines
    widget: list
    fields:
      - name: line
        label: Line
        widget: compute
        value_type: int
        value: '{{index}} + 1'
      - name: name
        label: Name
        widget: string
      - name: price
        label: Price
        widget: number
        value_type: float
      - name: quantity
        label: Quantity
        widget: number
      - name: amount
        label: Amount
        widget: compute
        value_type: float
        value: '{{fields.lines.*.price}} * {{fields.lines.*.quantity}}'
  - name: discount
    label: Discount (%)
    widget: number
    required: false
  - name: total
    label: Total
    widget: compute
    value_type: float
    value: "round(sum({{fields.lines.*.amount}}) * (1 - {{fields.discount | default('0')}} / 100), 2)"
```

```toml TOML
[[fields]]
name = "lines"
label = "Lines"
widget = "list"

[[fields.fields]]
name = "line"
label = "Line"
widget = "compute"
value_type = "int"
value = "{{index}} + 1"

[[fields.fields]]
name = "name"
label = "Name"
widget = "string"

[[fields.fields]]
name = "price"
label = "Price"
widget = "number"
value_type = "float"

[[fields.fields]]
name = "quantity"
label = "Quantity"
widget = "number"

[[fields.fields]]
name = "amount"
label = "Amount"
widget = "compute"
value_type = "float"
value = "{{fields.lines.*.price}} * {{fields.lines.*.quantity}}"

[[fields]]
name = "discount"
label = "Discount (%)"
widget = "number"
required = false

[[fields]]
name = "total"
label = "Total"
widget = "compute"
value_type = "float"
value = "round(sum({{fields.lines.*.amount}}) * (1 - {{fields.discount | default('0')}} / 100), 2)"
```

```json JSON
{
  "fields": [
    {
      "name": "lines",
      "label": "Lines",
      "widget": "list",
      "fields": [
        {
          "name": "line",
          "label": "Line",
          "widget": "compute",
          "value_type": "int",
          "value": "{{index}} + 1"
        },
        {
          "name": "name",
          "label": "Name",
          "widget": "string"
        },
        {
          "name": "price",
          "label": "Price",
          "widget": "number",
          "value_type": "float"
        },
        {
          "name": "quantity",
          "label": "Quantity",
          "widget": "number"
        },
        {
          "name": "amount",
          "label": "Amount",
          "widget": "compute",
          "value_type": "float",
          "value": "{{fields.lines.*.price}} * {{fields.lines.*.quantity}}"
        }
      ]
    },
    {
      "name": "discount",
      "label": "Discount (%)",
      "widget": "number",
      "required": false
    },
    {
      "name": "total",
      "label": "Total",
      "widget": "compute",
      "value_type": "float",
      "value": "round(sum({{fields.lines.*.amount}}) * (1 - {{fields.discount | default('0')}} / 100), 2)"
    }
  ]
}
```

```js JavaScript
{
  fields: [
    {
      name: 'lines',
      label: 'Lines',
      widget: 'list',
      fields: [
        {
          name: 'line',
          label: 'Line',
          widget: 'compute',
          value_type: 'int',
          value: '{{index}} + 1',
        },
        {
          name: 'name',
          label: 'Name',
          widget: 'string',
        },
        {
          name: 'price',
          label: 'Price',
          widget: 'number',
          value_type: 'float',
        },
        {
          name: 'quantity',
          label: 'Quantity',
          widget: 'number',
        },
        {
          name: 'amount',
          label: 'Amount',
          widget: 'compute',
          value_type: 'float',
          value: '{{fields.lines.*.price}} * {{fields.lines.*.quantity}}',
        },
      ],
    },
    {
      name: 'discount',
      label: 'Discount (%)',
      widget: 'number',
      required: false,
    },
    {
      name: 'total',
      label: 'Total',
      widget: 'compute',
      value_type: 'float',
      value:
        "round(sum({{fields.lines.*.amount}}) * (1 - {{fields.discount | default('0')}} / 100), 2)",
    },
  ],
}
```

Output example when two lines are added and `discount` is 20:

```yaml YAML
lines:
  - line: 1
    name: Beans
    price: 12.5
    quantity: 2
    amount: 25
  - line: 2
    name: Filters
    price: 4.25
    quantity: 3
    amount: 12.75
discount: 20
total: 30.2
```

```toml TOML
discount = 20
total = 30.2

[[lines]]
line = 1
name = "Beans"
price = 12.5
quantity = 2
amount = 25

[[lines]]
line = 2
name = "Filters"
price = 4.25
quantity = 3
amount = 12.75
```

```json JSON
{
  "lines": [
    {
      "line": 1,
      "name": "Beans",
      "price": 12.5,
      "quantity": 2,
      "amount": 25
    },
    {
      "line": 2,
      "name": "Filters",
      "price": 4.25,
      "quantity": 3,
      "amount": 12.75
    }
  ],
  "discount": 20,
  "total": 30.2
}
```
