Skip to content

join

The 'join' filter is designed to concatenate elements of an array into a single string, using a specified separator.

Functionality

  • Arrays: Takes an array of elements as input.
  • Separator: Uses a string argument as the separator between elements.
  • Output: Returns a single string with array elements concatenated, separated by the specified separator.

Syntax

    {{ array_variable | join: separator }}
Arguments

  • separator: A string representing the character or sequence of characters to insert between the elements of the array. Defaults to an empty string if not provided.

Code Samples

Example 1: Joining Array Elements with Commas

    {% assign fruits = "apple,banana,orange" | split: "," %}

    {{ fruits | join: ", " }}
Output:
apple, banana, orange
Example 2: Joining Array Elements with Custom Separator
    {% assign words = "Hello world how are you" | split: " " %}

    {{ words | join: "-" }}
Output:

Hello-world-how-are-you

Example 3: Joining Without a Separator (Default Behavior)

    {% assign numbers = "1,2,3" | split: "," %}

    {{ numbers | join }} 

Output:

123

Outliers and Special Cases

  • Empty Arrays: If the input array is empty, the join filter returns an empty string.
  • Non-Array Input: If the input is not an array, the join filter returns the original input value unchanged.

Key Points

  • The join filter is versatile for formatting lists and arrays into a readable string format.
  • It allows customization of the separator for different presentation styles.
  • It is particularly useful when constructing URLs, comma-separated values (CSV), or other delimited text formats.