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.
{{ html|raw|upper }}
<a href="/about">ABOUT & CONTACT</a>
upper and lower
Change text to upper or lower case.
{{ "Hello"|upper }} {{ "Hello"|lower }}
HELLO hello
capitalize
Makes the first character upper case and the rest lower case.
{{ "hELLO wORLD"|capitalize }}
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".
{{ "the QUICK brown fox"|title }}
The Quick Brown Fox
trim
Removes whitespace from both ends. Pass characters to remove instead, and "start" or "end" to trim one side.
{{ " hello "|trim }}|{{ "--hello--"|trim("-", "end") }}
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.
{{ "I like tea"|replace("tea", "coffee") }}
{{ "Hi NAME, welcome to PLACE"|replace({ NAME: "Ada", PLACE: "Kiln" }) }}
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.
{{ "a,b,c"|split(",")|join(" / ") }}
{{ "a,b,c"|split(",", 2)|join(" / ") }}
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.
{{ "Kiln is a template language"|truncate(13) }}
{{ "Short"|truncate(13) }}
Kiln is a tem…
Short
stripTags
Removes HTML tags and comments. It doesn't decode entities such as &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.
{{ "<p>Hello <b>world</b></p>"|stripTags }}
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.
{{ "Line one
Line two"|nl2br }}
Line one<br>
Line two
urlEncode
Encodes a string for use in a URL, or turns a hash into a query string.
/search?q={{ "tea & cake"|urlEncode }}
/search?{{ { q: "tea", page: 2 }|urlEncode }}
/search?q=tea%20%26%20cake
/search?q=tea&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.
<script>const page = {{ { title: "Home", tags: ["a", "b"] }|json|raw }};</script>
<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.
<a href="/tags/{{ "a&b"|escape("url") }}">Tag</a>
<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.
{{ html|raw }}
<em>Trusted</em>