Skip to content

capitalize

The capitalize filter is designed to modify the capitalization of letters within a string in the Experience Builder templating environment. Specifically, it capitalizes the first letter of the input string and any subsequent letters that follow whitespace, a hyphen, or a period.

Functionality

  • Strings: Takes a string as input.
  • Capitalization:
    • Uppercases the first letter of the string.
    • Uppercases any letters that immediately follow a whitespace character (space, tab, newline), a hyphen (-), or a period (.).
  • Output: Returns a new string with the modified capitalization.

Syntax

    {{ input_string | capitalize }}
Arguments

The capitalize filter does not require any arguments.

Code Samples

Example 1: Capitalizing the First Letter

    {% assign greeting = "hello, world!" %}
    {{ greeting | capitalize }}

Output:

Hello, world!
Example 2: Capitalizing After Punctuation

    {% assign sentence = "this is a sentence. and another one." %}
    {{ sentence | capitalize }}

Output:

This Is A Sentence. And Another One.
Example 3: Capitalizing After Hyphens

    {% assign title = "the-quick-brown-fox" %}
    {{ title | capitalize }}

Output:

The-Quick-Brown-Fox

Outliers and Special Cases

  • Empty Strings: If the input string is empty, the capitalize filter returns an empty string.
  • All Uppercase/Title Case: If the input string is already in all uppercase or title case, the filter might not make any noticeable changes.
  • Non-String Input: If the input is not a string, the capitalize filter might attempt to convert it to a string or return an error, depending on how Experience Builder handles type conversions.

Key Points

  • The capitalize filter is useful for formatting titles, sentences, or any text where specific capitalization rules need to be applied.
  • It goes beyond simply capitalizing the first letter by considering punctuation and hyphens.
  • For more granular control over capitalization, consider using other string manipulation techniques or custom filters.
  • If you only need to capitalize the very first letter of a string, you might achieve this with other built-in string functions or by using string slicing and concatenation.