# Text and formatting

Source: https://govform.com/resources/documentation/article/34-02-liquid-built-in-filters-n-z
Topic: Liquid Templates

Use these filters to prepare human-readable text and to escape values for HTML or XML. Choose the escaping filter for the output type: HTML, XML, JSON and URL output have different rules.

## Standard Liquid filters

These filters are provided as standard by the Liquid language. They are available anywhere Govform accepts a Liquid template.

| Filter | What it does | Example |
| --- | --- | --- |
| `append(string)` | **Appends string to input**  
Concatenates string to the end of input. | `{{ "Hello" | append: " World" }} → Hello World` |
| `capitalize()` | **Capitalizes first letter of string**  
Makes first character uppercase, rest lowercase. | `{{ "hello world" | capitalize }} → Hello world` |
| `downcase()` | **Converts string to lowercase**  
Makes all characters lowercase. | `{{ "HELLO" | downcase }} → hello` |
| `escape()` | **HTML escapes string**  
Escapes HTML special characters. | `{{ "<script>" | escape }} → &lt;script&gt;` |
| `escape_once()` | **HTML escapes string only if not already escaped**  
Prevents double-escaping of HTML. | `{{ "&lt;div&gt;" | escape_once }} → &lt;div&gt;` |
| `lstrip()` | **Removes whitespace from left side**  
Trims whitespace from beginning of string. | `{{ " hello" | lstrip }} → hello` |
| `newline_to_br()` | **Converts newlines to HTML breaks**  
Replaces \\n with <br> tags. | `{{ "line1\nline2" | newline_to_br }} → line1<br /> line2` |
| `normalize_whitespace()` | **Normalizes whitespace**  
Replaces each run of whitespace characters with one space. | `{{ "Hello World" | normalize_whitespace }} → Hello World` |
| `number_of_words(mode)` | **Counts words in text**  
Counts whitespace-separated words. Use "cjk" or "auto" to count CJK characters appropriately. | `{{ "one two three" | number_of_words }} → 3` |
| `prepend(string)` | **Prepends string to input**  
Concatenates string to beginning of input. | `{{ "World" | prepend: "Hello " }} → Hello World` |
| `remove(substring)` | **Removes all occurrences of substring**  
Removes every instance of substring from input. | `{{ "Hello World" | remove: "l" }} → Heo Word` |
| `remove_first(substring)` | **Removes first occurrence of substring**  
Removes only the first instance of substring. | `{{ "Hello World" | remove_first: "l" }} → Helo World` |
| `remove_last(substring)` | **Removes the last occurrence of a substring**  
Removes only the final instance of the supplied substring. | `{{ "Hello Hello" | remove_last: "Hello" }} → Hello` |
| `replace(search, replacement)` | **Replaces all occurrences of search with replacement**  
Substitutes every instance of search string. | `{{ "Hello World" | replace: "World", "Universe" }} → Hello Universe` |
| `replace_first(search, replacement)` | **Replaces first occurrence of search with replacement**  
Substitutes only the first instance of search string. | `{{ "Hello Hello" | replace_first: "Hello", "Hi" }} → Hi Hello` |
| `replace_last(search, replacement)` | **Replaces the last occurrence of text**  
Substitutes only the final instance of the search string. | `{{ "Hello Hello" | replace_last: "Hello", "World" }} → Hello World` |
| `rstrip()` | **Removes whitespace from right side**  
Trims whitespace from end of string. | `{{ "hello " | rstrip }} → hello` |
| `split(separator)` | **Splits string into array**  
Divides string at separator characters. | `{{ "a,b,c" | split: "," }} → ["a", "b", "c"]` |
| `strip()` | **Removes whitespace from both sides**  
Trims whitespace from beginning and end. | `{{ " hello " | strip }} → hello` |
| `strip_html()` | **Removes HTML tags from string**  
Strips all HTML markup, leaving text content. | `{{ "<p>Hello</p>" | strip_html }} → Hello` |
| `strip_newlines()` | **Removes newline characters**  
Removes \\n and \\r characters from string. | `{{ "line1\nline2" | strip_newlines }} → line1line2` |
| `truncate(length, ellipsis)` | **Truncates string to maximum length**  
Cuts string at length, optionally adding ellipsis. | `{{ "Hello World" | truncate: 8, "..." }} → Hello...` |
| `truncatewords(word_count, ellipsis)` | **Truncates to maximum word count**  
Cuts string after specified number of words. | `{{ "Hello World Test" | truncatewords: 2 }} → Hello World...` |
| `upcase()` | **Converts string to uppercase**  
Makes all characters uppercase. | `{{ "hello" | upcase }} → HELLO` |
| `xml_escape()` | **Escapes text for XML**  
Replaces XML special characters with safe entity references. | `{{ "<item>A & B</item>" | xml_escape }}` |

## Govform filters

These filters are added by Govform for service-building and data-handling tasks.

| Filter | What it does | Example |
| --- | --- | --- |
| `decompress_text(base64_brotli_string)` | **Decompresses Brotli-compressed text stored as base64**  
Decodes a base64 string containing Brotli-compressed UTF-8 text, such as HTML generated by the Generate HTML action. | `{{ actions.myHtmlAction.compressedHtml | decompress_text }}` |
| `field_to_text(field_value, field_id, field_defs, part_id)` | **Converts field value to display text**  
Formats field value according to field type for display. | `{{ my_field_value | field_to_text: "field1", form.fieldDefs }}` |
| `form_to_text(form, newlines)` | **Converts form data to formatted text**  
Creates human-readable text summary of form data. | `{{ form | form_to_text: 2 }}` |
| `lookup_item_id(item_text, field_id)` | **Gets the ID for a choice item by display text**  
Reverse lookup to find choice ID from display text. | `{{ "Option A" | lookup_item_id: "my_field" }}` |
| `lookup_item_label(item_id, field_id)` | **Gets the display text for a choice item by ID**  
Looks up choice text from field definition using the choice ID. | `{{ "choice1" | lookup_item_label: "my_field" }}` |
| `remove_empty_strings(object)` | **Removes empty strings from objects recursively**  
Cleans objects and arrays by removing properties whose value is an empty string. | `{{ form_data | remove_empty_strings }}` |

## Related guidance

-   [Write Liquid templates](<https://govform.com/resources/documentation/article/11-write-liquid-templates>)
-   [Use Markdown and HTML](<https://govform.com/resources/documentation/article/12-use-markdown-and-html>)
