# String filters

> Filters for changing and encoding text.

These filters change and encode text. Apply a filter with `|`, as in `{{ title|upper }}`. Lengths and positions count Unicode code points, not JavaScript's UTF-16 units. A letter written with a separate accent mark, or an emoji made of several code points, counts as more than one.

Most string filters return an empty string for null, so optional fields print nothing.

## Safe markup

Given safe markup, such as the content of [`apply`](https://kilnlang.com/docs/tags/apply/) or a value marked with `raw`, these filters return safe markup too: `upper`, `lower`, `capitalize`, `title`, `trim`, `replace`, `truncate`, `stripTags` and `nl2br`. They change only the text. Tags and entities stay as they are, and any text they add, such as a replacement, is escaped. Other filters return plain values, which are escaped when printed.

page.kiln:

```kiln
{{ html|raw|upper }}
```

Output:

```html
<a href="/about">ABOUT &amp; CONTACT</a>
```

## `upper` and `lower`

Change text to upper or lower case.

page.kiln:

```kiln
{{ "Hello"|upper }} {{ "Hello"|lower }}
```

Output:

```html
HELLO hello
```

## `capitalize`

Makes the first character upper case and the rest lower case.

page.kiln:

```kiln
{{ "hELLO wORLD"|capitalize }}
```

Output:

```html
Hello world
```

## `title`

Starts each word with an upper-case letter and makes the rest lower case. Accent marks and apostrophes belong to the word they are in, so "o'neil" becomes "O'neil".

page.kiln:

```kiln
{{ "the QUICK brown fox"|title }}
```

Output:

```html
The Quick Brown Fox
```

## `trim`

Removes whitespace from both ends. Pass characters to remove instead, and `"start"` or `"end"` to trim one side.

page.kiln:

```kiln
{{ "  hello  "|trim }}|{{ "--hello--"|trim("-", "end") }}
```

Output:

```html
hello|--hello
```

## `replace`

Replaces every match of a piece of text. Pass a hash to replace several at once, and the longest match wins at each position. It matches plain text, not regular expressions.

page.kiln:

```kiln
{{ "I like tea"|replace("tea", "coffee") }}
{{ "Hi NAME, welcome to PLACE"|replace({ NAME: "Ada", PLACE: "Kiln" }) }}
```

Output:

```html
I like coffee
Hi Ada, welcome to Kiln
```

## `split`

Splits text into an array. An optional limit caps the number of items. An empty separator splits into characters.

page.kiln:

```kiln
{{ "a,b,c"|split(",")|join(" / ") }}
{{ "a,b,c"|split(",", 2)|join(" / ") }}
```

Output:

```html
a / b / c
a / b
```

## `truncate`

Shortens text to a length, trims trailing spaces and adds an ellipsis if it cut anything. Pass a different ending as the second argument. Given safe markup, it counts only the text and closes any tags the cut leaves open.

page.kiln:

```kiln
{{ "Kiln is a template language"|truncate(13) }}
{{ "Short"|truncate(13) }}
```

Output:

```html
Kiln is a tem…
Short
```

## `stripTags`

Removes HTML tags and comments. It doesn't decode entities such as `&amp;amp;`, and removing a tag never leaves a new one behind. Its result is still escaped, so don't mark it with `raw` to make it safe.

page.kiln:

```kiln
{{ "<p>Hello <b>world</b></p>"|stripTags }}
```

Output:

```html
Hello world
```

## `nl2br`

Escapes the text, then adds a `<br>` before each line break. Safe markup isn't escaped again. The result is safe HTML.

page.kiln:

```kiln
{{ "Line one
Line two"|nl2br }}
```

Output:

```html
Line one<br>
Line two
```

## `urlEncode`

Encodes a string for use in a URL, or turns a hash into a query string.

page.kiln:

```kiln
/search?q={{ "tea & cake"|urlEncode }}
/search?{{ { q: "tea", page: 2 }|urlEncode }}
```

Output:

```html
/search?q=tea%20%26%20cake
/search?q=tea&amp;page=2
```

## `json`

Turns a value into JSON. Pass a number to indent it. Sets, generators and other iterables become arrays. The output escapes `<`, `>` and `&`, so it is safe inside a `<script>` tag with `raw`.

page.kiln:

```kiln
<script>const page = {{ { title: "Home", tags: ["a", "b"] }|json|raw }};</script>
```

Output:

```html
<script>const page = {"title":"Home","tags":["a","b"]};</script>
```

## `escape`

Escapes a value with a strategy: `"html"` (the default), `"attr"`, `"js"`, `"css"` or `"url"`. See [Escaping](https://kilnlang.com/docs/language/escaping/).

page.kiln:

```kiln
<a href="/tags/{{ "a&b"|escape("url") }}">Tag</a>
```

Output:

```html
<a href="/tags/a%26b">Tag</a>
```

## `raw`

Marks a value as safe, so it is output without escaping. Only use it for HTML you trust. Put it after filters that return plain values, such as `split` and `join`.

page.kiln:

```kiln
{{ html|raw }}
```

Output:

```html
<em>Trusted</em>
```
