Skip to content

Search tags, filters, functions and guides.

Reference / String filters

Reference

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 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
{{ html|raw|upper }}
Output
<a href="/about">ABOUT &amp; CONTACT</a>

upper and lower

Change text to upper or lower case.

page.kiln
{{ "Hello"|upper }} {{ "Hello"|lower }}
Output
HELLO hello

capitalize

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

page.kiln
{{ "hELLO wORLD"|capitalize }}
Output
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
{{ "the QUICK brown fox"|title }}
Output
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
{{ "  hello  "|trim }}|{{ "--hello--"|trim("-", "end") }}
Output
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
{{ "I like tea"|replace("tea", "coffee") }}
{{ "Hi NAME, welcome to PLACE"|replace({ NAME: "Ada", PLACE: "Kiln" }) }}
Output
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
{{ "a,b,c"|split(",")|join(" / ") }}
{{ "a,b,c"|split(",", 2)|join(" / ") }}
Output
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 is a template language"|truncate(13) }}
{{ "Short"|truncate(13) }}
Output
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
{{ "<p>Hello <b>world</b></p>"|stripTags }}
Output
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
{{ "Line one
Line two"|nl2br }}
Output
Line one<br>
Line two

urlEncode

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

page.kiln
/search?q={{ "tea & cake"|urlEncode }}
/search?{{ { q: "tea", page: 2 }|urlEncode }}
Output
/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
<script>const page = {{ { title: "Home", tags: ["a", "b"] }|json|raw }};</script>
Output
<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.

page.kiln
<a href="/tags/{{ "a&b"|escape("url") }}">Tag</a>
Output
<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
{{ html|raw }}
Output
<em>Trusted</em>