Skip to content

Compute Field ​

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

Preview ​

A read-only display of the computed value.

Data Type ​

A string representing the computed value.

If the 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.

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, 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 option is set to a number type, it’s an arithmetic expression 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 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, 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, 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, including in an Object field 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. 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.
  • A template tag escaped with a backslash, e.g. \{{ page.url }}, which is output as is without the backslash. See 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 ​

  • Type: string
  • Default: string

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

  • string: The value option is a template, and the result is stored as a string.
  • int: The value option is an arithmetic expression, 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 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. 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 or 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
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
[[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
{
  "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
{
  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
first_name: John
last_name: Doe
full_name: John Doe
toml
first_name = "John"
last_name = "Doe"
full_name = "John Doe"
json
{
  "first_name": "John",
  "last_name": "Doe",
  "full_name": "John Doe"
}

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

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
[[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
{
  "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
{
  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 “[email protected]”:

yaml
contact_email: [email protected]
contact_email_link: mailto:[email protected]?subject=Inquiry
toml
contact_email = "[email protected]"
contact_email_link = "mailto:[email protected]?subject=Inquiry"
json
{
  "contact_email": "[email protected]",
  "contact_email_link": "mailto:[email protected]?subject=Inquiry"
}

Generating a Description ​

This example generates a description from the beginning of a RichText field named body. The plainify transformation removes the Markdown syntax and HTML tags, and truncate then shortens the result to 50 characters.

yaml
fields:
  - name: body
    label: Body
    widget: richtext
  - name: description
    label: Description
    widget: compute
    value: "{{fields.body | plainify | truncate(50, '...')}}"
toml
[[fields]]
name = "body"
label = "Body"
widget = "richtext"
[[fields]]
name = "description"
label = "Description"
widget = "compute"
value = "{{fields.body | plainify | truncate(50, '...')}}"
json
{
  "fields": [
    {
      "name": "body",
      "label": "Body",
      "widget": "richtext"
    },
    {
      "name": "description",
      "label": "Description",
      "widget": "compute",
      "value": "{{fields.body | plainify | truncate(50, '...')}}"
    }
  ]
}
js
{
  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
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
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
{
  "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
fields:
  - name: items
    label: Items
    widget: list
    fields:
      - name: name
        label: Name
        widget: string
      - name: index
        label: Item Index
        widget: compute
        value: '{{index}}'
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
{
  "fields": [
    {
      "name": "items",
      "label": "Items",
      "widget": "list",
      "fields": [
        {
          "name": "name",
          "label": "Name",
          "widget": "string"
        },
        {
          "name": "index",
          "label": "Item Index",
          "widget": "compute",
          "value": "{{index}}"
        }
      ]
    }
  ]
}
js
{
  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
items:
  - name: Apple
    index: 0
  - name: Banana
    index: 1
  - name: Cherry
    index: 2
toml
[[items]]
name = "Apple"
index = 0
[[items]]
name = "Banana"
index = 1
[[items]]
name = "Cherry"
index = 2
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
fields:
  - name: title
    label: Title
    widget: string
  - name: id
    label: ID
    widget: compute
    value: '{{fields.title | slugify}}-{{uuid_shorter}}'
toml
[[fields]]
name = "title"
label = "Title"
widget = "string"
[[fields]]
name = "id"
label = "ID"
widget = "compute"
value = "{{fields.title | slugify}}-{{uuid_shorter}}"
json
{
  "fields": [
    {
      "name": "title",
      "label": "Title",
      "widget": "string"
    },
    {
      "name": "id",
      "label": "ID",
      "widget": "compute",
      "value": "{{fields.title | slugify}}-{{uuid_shorter}}"
    }
  ]
}
js
{
  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
title: Hello World
id: hello-world-e7bc7d91
toml
title = "Hello World"
id = "hello-world-e7bc7d91"
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
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
[[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
{
  "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
{
  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
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
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
{
  "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
}