Skip to content

String Transforms

Normative reference

See the Transforms reference for the normative definition of each transform below, plus worked examples.

All 25 transforms below coerce their input to a string first (the same rule {{ }} interpolation uses), unless noted otherwise.

Name Arguments Description
upcase — Uppercases the input.
downcase — Lowercases the input.
append: value Concatenates value (also string-coerced) onto the end of the input.
prepend: value Concatenates value onto the start of the input.
contains: value Returns true/false. For list input, matches any element whose string form equals value's string form; otherwise, substring match against the input coerced to a string.
remove: value Removes every occurrence of value from the input.
remove_first: value Removes only the first occurrence of value.
remove_last: value Removes only the last occurrence of value.
replace: old new Replaces every occurrence of old with new.
replace_first: old new Replaces only the first occurrence of old with new. Empty old inserts new at the start.
replace_last: old new Replaces only the last occurrence of old with new. Empty old appends new at the end.
lstrip — Trims leading Unicode whitespace.
rstrip — Trims trailing Unicode whitespace.
strip — Trims leading and trailing Unicode whitespace.
strip_html — Removes <script> / <style> bodies (case-insensitive), HTML comments, and all remaining tags. HTML entities are not decoded.
strip_newlines — Removes every CR (\r) and LF (\n) character.
escape — HTML-encodes &, <, >, ", ' as entities, including a metacharacter inside an existing entity (&amp; → &amp;amp;).
escape_once — Same as escape, but leaves an & that already begins a valid named or numeric entity untouched.
slice: offset (length?) With one argument returns one character at offset; with two arguments returns length characters starting at offset. Negative offset counts from the end. Offsets fully outside the string yield "". length is clamped to remaining input; a negative length is a degenerate-value soft failure.
split: delimiter Returns a list of substrings separated by delimiter. Empty delimiter splits character-by-character. Result is a list, so it composes with each and contains.
truncate: length (ellipsis?) If length >= input.length, returns input unchanged. Otherwise returns the first length - ellipsis.length characters followed by ellipsis (default "..."). If length < ellipsis.length, returns the first length characters of ellipsis. A negative length is a degenerate-value soft failure.
url_decode — Reverses form-encoding: + → space, %HH (any hex case) → byte; resulting bytes decoded as UTF-8. Malformed percent-sequences are an error.
url_encode — Form-encodes (application/x-www-form-urlencoded): space → +; unreserved set [A-Za-z0-9-_.~] preserved; everything else percent-encoded as UTF-8 (uppercase hex).
url_escape — RFC 3986 percent-encodes: space → %20; unreserved set preserved; reserved set :/?#[]@!$&'()*+,;= preserved; everything else percent-encoded as UTF-8.
url_param_escape — Percent-encodes like url_escape but additionally encodes & and =, so the result is safe as a single query-string parameter value.

Examples

{{ "Hello" | upcase }}                       {{! HELLO }}
{{ "  padded  " | strip }}                    {{! padded }}
{{ "a,b,,c" | split: "," | join: "|" }}       {{! a|b||c }}
{{ "<b>bold</b>" | strip_html }}              {{! bold }}
{{ "Tom & Jerry" | escape }}                  {{! Tom &amp; Jerry }}
{{ "hello world" | slice: 0 5 }}              {{! hello }}
{{ "a long sentence" | truncate: 10 }}        {{! a long... }}

Where to next