Skip to content

prepend

The prepend filter is designed to concatenate a specified string to the beginning of the input string.

Functionality

  • Strings: Takes a string as input.
  • Prefix: Requires a string argument to add to the beginning.
  • Output: Returns a new string with the prefix string added at the start.

Syntax

    {{ input_string | prepend: string_to_prepend }}
Arguments

  • string_to_prepend: The string that you want to add to the beginning of the input_string.

Code Samples

Example 1: Prepending a Title

    {% assign name = "John Doe" %}
    {{ name | prepend: "Mr. " }}

Output:

Mr. John Doe
Example 2: Prepending a Prefix

    {% assign code = "12345" %}

    {{ code | prepend: "SKU-" }}
Output:
SKU-12345
Example 3: Prepending an Empty String

    {% assign message = "Hello, world!" %}

    {{ message | prepend: "" }} 
Output:
Hello, world!  (No change)

Outliers and Special Cases

  • Empty Input String: If the input string is empty, the prepended string becomes the entire output.
  • Non-String Input: If the input is not a string, the prepend filter might attempt to convert it to a string or return an error, depending on how Experience Builder handles type conversions.
  • Multiple Prepends: You can chain multiple prepend filters to add multiple strings to the beginning.

Key Points

  • The prepend filter is a simple yet powerful tool for dynamically constructing strings in templates.
  • It can be used for adding titles, prefixes, or any other string that needs to be placed at the start of another string.
  • If you want to add a string to the end, use the append filter instead.
  • Consider using the format filter or string interpolation for more complex string formatting tasks.