---
title: String Transformations
description: Use string transformations in Sveltia CMS to manipulate and format content values.
---

String transformations allow manipulating and formatting string values in your content entries. These transformations can be applied in various contexts, such as generating summaries or formatting dates.

:::tip[Note for Netlify/Decap CMS users]

In Netlify/Decap CMS, this feature is known as **summary string transformations**. We simply refer to them as **string transformations** because, in Sveltia CMS, they can be used in multiple contexts beyond just entry summaries.

:::

## Available Contexts

String transformations can be applied in the following contexts:

- [Entry Collection](/docs/collections/entries): the `summary`, `slug`, `path` and `preview_path` options
- [Compute Field](/docs/fields/compute): the `value` option
- [Hidden Field](/docs/fields/hidden): the `default` option
- [List Field](/docs/fields/list): the `summary` option
- [Object Field](/docs/fields/object): the `summary` option
- [Editor Components](/docs/api/editor-components): the `summary` option

:::info[Future Plans]

More contexts may be added in future releases.

:::

## Syntax

String transformations are applied using the following syntax:

```
{{value | transformation_name}}
```

or with arguments:

```
{{value | transformation_name(arguments)}}
```

Where:

- `value`: The original string value to be transformed. This is typically a field name enclosed in double curly braces (e.g. `{{title}}`).
- `transformation_name`: The name of the transformation to apply (e.g. `upper`, `lower`, `truncate`).
- `arguments`: Optional arguments required by certain transformations, enclosed in parentheses.

Multiple transformations can be chained together by separating them with a pipe (`|`). The output of one transformation serves as the input for the next. For example:

```
{{value | transformation1 | transformation2(arguments)}}
```

### Notes on Syntax Rules

There are some syntax rules to keep in mind when using string transformations:

- String arguments must be enclosed in single quotes.
- Numeric arguments should not be quoted.
- No spaces are allowed between the transformation name and the opening parenthesis.

Spaces inside the curly braces and around the pipe character (`|`) are optional, so `{{ title | upper }}` and `{{title|upper}}` are the same as `{{title | upper}}`. This applies to every template tag in the configuration, not only the ones with transformations, e.g. `{{ slug }}` in the `path` option or `{{ locale }}` in the `folder` option.

### Escaping Template Tags

To output a template tag as is rather than have Sveltia CMS fill it in, put a backslash (`\`) right before the opening curly braces. The backslash is removed, and the rest of the tag is kept verbatim. This is useful when a value contains a template for another tool that uses the same double curly brace syntax, such as a static site generator like [Eleventy](https://www.11ty.dev/) or [Hugo](https://gohugo.io/). For example, `portfolio/\{{ title | slugify }}/index.html` becomes `portfolio/{{ title | slugify }}/index.html`.

In a string format where the backslash is itself an escape character, write two backslashes (`\\{{`) so that one remains in the value. This applies to double-quoted strings in YAML and TOML, JSON and JavaScript. A single-quoted or unquoted YAML string takes a single backslash.

Escaping is supported in all the [available contexts](#available-contexts) except the `slug` and `path` options of an entry collection.

## Available Transformations

### `upper`

Transform the string to uppercase.

```
{{value | upper}}
```

Configuration example:

```yaml YAML
summary: '{{title | upper}}'
```

```toml TOML
summary = "{{title | upper}}"
```

```json JSON
{
  "summary": "{{title | upper}}"
}
```

```js JavaScript
{
  summary: "{{title | upper}}",
}
```

### `lower`

Transform the string to lowercase.

```
{{value | lower}}
```

Configuration example:

```yaml YAML
summary: '{{title | lower}}'
```

```toml TOML
summary = "{{title | lower}}"
```

```json JSON
{
  "summary": "{{title | lower}}"
}
```

```js JavaScript
{
  summary: "{{title | lower}}",
}
```

### `slugify`

Convert the string to a URL-friendly slug.

```
{{value | slugify}}
```

The transformation converts the string based on the [global slug options](/docs/collections/entries/slugs#global-slug-options).

Configuration example:

```yaml YAML
summary: '{{title | slugify}}'
```

```toml TOML
summary = "{{title | slugify}}"
```

```json JSON
{
  "summary": "{{title | slugify}}"
}
```

```js JavaScript
{
  summary: "{{title | slugify}}",
}
```

For example, if the `title` field has the value `Hello, World! This is a Test`, the result will be:

```
hello-world-this-is-a-test
```

### `plainify`

Convert a Markdown or HTML string to plain text by removing the Markdown syntax and HTML tags. Line breaks are collapsed into spaces, and character references like `&amp;` are decoded.

```
{{value | plainify}}
```

This is useful for generating a description or an excerpt from a [RichText field](/docs/fields/richtext) with a [Compute field](/docs/fields/compute).

Configuration example:

```yaml YAML
value: "{{fields.body | plainify | truncate(160, '...')}}"
```

```toml TOML
value = "{{fields.body | plainify | truncate(160, '...')}}"
```

```json JSON
{
  "value": "{{fields.body | plainify | truncate(160, '...')}}"
}
```

```js JavaScript
{
  value: "{{fields.body | plainify | truncate(160, '...')}}",
}
```

For example, if the `body` field has the value `Let’s **welcome** our [guest](https://example.com)`, the result will be:

```
Let’s welcome our guest
```

Put `plainify` before `truncate` so the limit applies to the visible text and the Markdown syntax isn’t cut in the middle.

:::info[Notes]

- The value is always parsed as Markdown and HTML, so only use `plainify` on fields that contain them. A plain string can lose text, e.g. `Vec<T>` becomes `Vec`, and `1. Introduction` becomes `Introduction`.
- Images are removed along with their alternative text. Syntax that isn’t Markdown or HTML, like Hugo shortcodes, is left as is.
- Unlike Hugo’s `plainify` function, which only removes HTML tags, this transformation also removes the Markdown syntax.
- The result is plain text, not sanitized HTML. It can contain markup after decoding, e.g. `&lt;img&gt;` becomes `<img>`, so escape it in your site templates.
- A [List](/docs/fields/list) or [Object](/docs/fields/object) field’s `summary` already shows a RichText field as plain text, so `plainify` isn’t needed there.

:::

### `truncate`

Truncate the string to a specified length, adding a suffix if anything is cut off.

```
{{value | truncate(<length>)}}
```

```
{{value | truncate(<length>, '<suffix>')}}
```

The `length` argument specifies the maximum number of characters to keep. If the string exceeds this length, it’s truncated, and the suffix is appended.

The optional `suffix` argument defaults to an ellipsis (`…`). Specify another string to use instead, e.g. `'...'`, or an empty string (`''`) to cut the string without adding anything.

Configuration examples:

```yaml YAML
summary: '{{content | truncate(100)}}'
```

```toml TOML
summary = "{{content | truncate(100)}}"
```

```json JSON
{
  "summary": "{{content | truncate(100)}}"
}
```

```js JavaScript
{
  summary: "{{content | truncate(100)}}",
}
```

```yaml YAML
summary: "{{content | truncate(100, '...')}}"
```

```toml TOML
summary = "{{content | truncate(100, '...')}}"
```

```json JSON
{
  "summary": "{{content | truncate(100, '...')}}"
}
```

```js JavaScript
{
  summary: "{{content | truncate(100, '...')}}",
}
```

### `default`

Provide a default value if the original value is null or empty.

```
{{value | default('<default_value>')}}
```

The `default_value` is returned if the original value is null or an empty string; otherwise, the original value is returned. The `default_value` can also be an empty string, e.g. `default('')`.

Configuration example:

```yaml YAML
summary: "{{description | default('No description available.')}}"
```

```toml TOML
summary = "{{description | default('No description available.')}}"
```

```json JSON
{
  "summary": "{{description | default('No description available.')}}"
}
```

```js JavaScript
{
  summary: "{{description | default('No description available.')}}",
}
```

It’s possible to fall back to another field’s value using a nested template:

```yaml YAML
preview_path: "/{{fields.slug | default('{{fields.title}}')}}/"
```

```toml TOML
preview_path = "/{{fields.slug | default('{{fields.title}}')}}/"
```

```json JSON
{
  "preview_path": "/{{fields.slug | default('{{fields.title}}')}}/"
}
```

```js JavaScript
{
  preview_path: "/{{fields.slug | default('{{fields.title}}')}}/",
}
```

A nested template can also combine several field values with other text:

```yaml YAML
summary: "{{fields.subtitle | default('{{fields.title}} by {{fields.author}}')}}"
```

```toml TOML
summary = "{{fields.subtitle | default('{{fields.title}} by {{fields.author}}')}}"
```

```json JSON
{
  "summary": "{{fields.subtitle | default('{{fields.title}} by {{fields.author}}')}}"
}
```

```js JavaScript
{
  summary: "{{fields.subtitle | default('{{fields.title}} by {{fields.author}}')}}",
}
```

### `ternary`

Choose between two values based on the truthiness of the original value.

```
{{value | ternary('<true_value>', '<false_value>')}}
```

The `true_value` is returned if the original value is truthy; otherwise, the `false_value` is returned.

Configuration examples:

```yaml YAML
summary: "{{is_private | ternary('Private', 'Public')}}: {{title}}"
```

```toml TOML
summary = "{{is_private | ternary('Private', 'Public')}}: {{title}}"
```

```json JSON
{
  "summary": "{{is_private | ternary('Private', 'Public')}}: {{title}}"
}
```

```js JavaScript
{
  summary: "{{is_private | ternary('Private', 'Public')}}: {{title}}",
}
```

```yaml YAML
summary: "{{title}} – {{featured | ternary('Featured', 'Regular')}} Event"
```

```toml TOML
summary = "{{title}} – {{featured | ternary('Featured', 'Regular')}} Event"
```

```json JSON
{
  "summary": "{{title}} – {{featured | ternary('Featured', 'Regular')}} Event"
}
```

```js JavaScript
{
  summary: "{{title}} – {{featured | ternary('Featured', 'Regular')}} Event",
}
```

```yaml YAML
summary: "{{title}} {{published | ternary('', '(DRAFT)')}}"
```

```toml TOML
summary = "{{title}} {{published | ternary('', '(DRAFT)')}}"
```

```json JSON
{
  "summary": "{{title}} {{published | ternary('', '(DRAFT)')}}"
}
```

```js JavaScript
{
  summary: "{{title}} {{published | ternary('', '(DRAFT)')}}",
}
```

As with the `default` transformation, the `true_value` and `false_value` can contain nested templates to use other field values. For example, show the subtitle after the title only when it’s available:

```yaml YAML
summary: "{{fields.subtitle | ternary('{{fields.title}}: {{fields.subtitle}}', '{{fields.title}}')}}"
```

```toml TOML
summary = "{{fields.subtitle | ternary('{{fields.title}}: {{fields.subtitle}}', '{{fields.title}}')}}"
```

```json JSON
{
  "summary": "{{fields.subtitle | ternary('{{fields.title}}: {{fields.subtitle}}', '{{fields.title}}')}}"
}
```

```js JavaScript
{
  summary: "{{fields.subtitle | ternary('{{fields.title}}: {{fields.subtitle}}', '{{fields.title}}')}}",
}
```

### `date`

Format a date string, with an optional timezone.

```
{{value | date('<format>')}}
```

```
{{value | date('<format>', '<timezone>')}}
```

The `format` argument specifies the desired date format using [Day.js formatting tokens](https://day.js.org/docs/en/display/format).

The optional `timezone` argument allows you to specify the timezone for formatting. It only supports `utc` for Coordinated Universal Time. If no timezone is provided, the local timezone will be used.

If the value comes from a [DateTime](/docs/fields/datetime) field with a custom `format` option, such as `DD/MM/YYYY` or `X` (Unix timestamp), it’s parsed in that format. If an invalid date is provided, an empty string will be returned.

Configuration examples:

```yaml YAML
summary: "{{publish_date | date('YYYY-MM-DD')}}"
```

```toml TOML
summary = "{{publish_date | date('YYYY-MM-DD')}}"
```

```json JSON
{
  "summary": "{{publish_date | date('YYYY-MM-DD')}}"
}
```

```js JavaScript
{
  summary: "{{publish_date | date('YYYY-MM-DD')}}",
}
```

```yaml YAML
summary: "{{publish_date | date('YYYY-MM-DD', 'utc')}}"
```

```toml TOML
summary = "{{publish_date | date('YYYY-MM-DD', 'utc')}}"
```

```json JSON
{
  "summary": "{{publish_date | date('YYYY-MM-DD', 'utc')}}"
}
```

```js JavaScript
{
  summary: "{{publish_date | date('YYYY-MM-DD', 'utc')}}",
}
```

:::warning[Breaking change from Netlify/Decap CMS]

Sveltia CMS (and Decap CMS 3.1.1) replaced the Moment.js library with Day.js for date formatting and parsing. Since [Day.js tokens](https://day.js.org/docs/en/display/format) are not 100% compatible with [Moment.js tokens](https://momentjs.com/docs/#/displaying/format/), this could be a breaking change in certain cases. Check your date/time format if you’re migrating from Netlify CMS or earlier versions of Decap CMS.

:::

## Custom Transformations

In addition to the built-in transformations, you can register your own with the `CMS.registerStringTransformation()` method, and use them like `{{title | reverse}}` or `{{title | wrap('[', ']')}}`. See [Custom String Transformations](/docs/api/string-transformations) for details.

## Examples

### Summary with Multiple Transformations

The below example demonstrates how to use multiple string transformations in the `summary` option of a collection:

```yaml YAML
collections:
  - name: blog
    label: Blog
    summary: "{{title | upper}} — {{publish_date | date('YYYY-MM-DD')}} — {{body | truncate(20, '...')}}"
    fields:
      - name: title
        label: Title
        widget: string
      - name: publish_date
        label: Publish Date
        widget: datetime
      - name: body
        label: Body
        widget: richtext
```

```toml TOML
[[collections]]
name = "blog"
label = "Blog"
summary = "{{title | upper}} — {{publish_date | date('YYYY-MM-DD')}} — {{body | truncate(20, '...')}}"

[[collections.fields]]
name = "title"
label = "Title"
widget = "string"

[[collections.fields]]
name = "publish_date"
label = "Publish Date"
widget = "datetime"

[[collections.fields]]
name = "body"
label = "Body"
widget = "richtext"
```

```json JSON
{
  "collections": [
    {
      "name": "blog",
      "label": "Blog",
      "summary": "{{title | upper}} — {{publish_date | date('YYYY-MM-DD')}} — {{body | truncate(20, '...')}}",
      "fields": [
        {
          "name": "title",
          "label": "Title",
          "widget": "string"
        },
        {
          "name": "publish_date",
          "label": "Publish Date",
          "widget": "datetime"
        },
        {
          "name": "body",
          "label": "Body",
          "widget": "richtext"
        }
      ]
    }
  ]
}
```

```js JavaScript
{
  collections: [
    {
      name: "blog",
      label: "Blog",
      summary: "{{title | upper}} — {{publish_date | date('YYYY-MM-DD')}} — {{body | truncate(20, '...')}}",
      fields: [
        {
          name: "title",
          label: "Title",
          widget: "string",
        },
        {
          name: "publish_date",
          label: "Publish Date",
          widget: "datetime",
        },
        {
          name: "body",
          label: "Body",
          widget: "richtext",
        },
      ],
    },
  ],
}
```

It will transform the `title` to uppercase, format the `publish_date` to `YYYY-MM-DD` and truncate the `body` to 20 characters with an ellipsis. For example, if an entry has the following values:

```yaml YAML
title: My First Blog Post
publish_date: 2024-06-15T10:30:00Z
body: This is the content of my first blog post. It has a lot of interesting information
```

```toml TOML
title = "My First Blog Post"
publish_date = 2024-06-15T10:30:00Z
body = "This is the content of my first blog post. It has a lot of interesting information"
```

```json JSON
{
  "title": "My First Blog Post",
  "publish_date": "2024-06-15T10:30:00Z",
  "body": "This is the content of my first blog post. It has a lot of interesting information"
}
```

The resulting summary will be:

```
MY FIRST BLOG POST — 2024-06-15 — This is the content...
```

### Chaining Transformations

You can chain multiple transformations together by separating them with a pipe (`|`). For example, to convert a title to uppercase and then truncate it to 10 characters, you can use:

```yaml YAML
summary: "{{title | upper | truncate(10, '...')}}"
```

```toml TOML
summary = "{{title | upper | truncate(10, '...')}}"
```

```json JSON
{
  "summary": "{{title | upper | truncate(10, '...')}}"
}
```

```js JavaScript
{
  summary: "{{title | upper | truncate(10, '...')}}",
}
```
