Skip to content

handle

The handle filter (also aliased as handleize) transforms an input string into a URL-friendly and human-readable "handle" or "slug." It is particularly useful when generating clean and meaningful identifiers for resources like blog posts, products, or categories.

Functionality

  • Strings: Takes a string as input.
  • Normalization:
    • Converts all letters to lowercase.
    • Replaces spaces, punctuation, and other special characters with hyphens.
    • Removes any remaining invalid characters that are not letters, numbers, or hyphens.
    • Ensures consecutive capital letters are separated by a hyphen.
  • Output: Returns a new string that is suitable for use in URLs or as a human-readable identifier.

Syntax

    {{ input_string | handle }} 

or

    {{ input_string | handleize }} 

(Both syntaxes are equivalent)

Arguments

The handle filter does not require any arguments.

Code Samples

Example 1: Creating a URL Slug

    {% assign title = "My Awesome Blog Post" %}

    {{ title | handle }}
Output:
my-awesome-blog-post
Example 2: Handling Special Characters

    {% assign product_name = "Product with !@#$%^&*() characters" %}

    {{ product_name | handle }}

Output:

product-with-characters
Example 3: Handling Accents and Diacritics
    {% assign name = "José Ramón López" %}

    {{ name | handle }}

Output: (Depending on the underlying normalization rules)

jose-ramon-lopez

Outliers and Special Cases

  • Empty Strings: If the input string is empty, the handle filter returns an empty string.
  • Non-String Input: If the input is not a string, the filter might attempt to convert it to a string or return an error.

Key Points

  • The handle filter streamlines the process of creating URL-friendly identifiers (slugs).
  • It improves the readability of generated handles by replacing special characters with hyphens.
  • The filter can be customized by modifying the Handleize method in the MiscFilters class if you have specific requirements for handling certain characters.
  • Consider using the handle filter whenever you need to generate clean, concise identifiers for resources within your Experience Builder templates.