View Source Solid.Filter (solid v0.17.1)

Standard filters

Summary

Functions

Returns the absolute value of a number.

Concatenates two strings and returns the concatenated value.

Apply filter if it exists. Otherwise return the first input.

Limits a number to a minimum value.

Limits a number to a maximum value.

Decodes a string in Base64 format.

Encodes a string to Base64 format.

Decodes a string in URL-safe Base64 format.

Encodes a string to URL-safe Base64 format.

Makes the first character of a string capitalized.

Rounds the input up to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.

Removes all occurrences of nil from a list

Concatenates (joins together) multiple arrays. The resulting array contains all the items from the input arrays.

Converts a DateTime/NaiveDateTime struct into another date format. The input may also be a Unix timestamp or an ISO 8601 date string.

Allows you to specify a fallback in case a value doesn’t exist. default will show its value if the left side is nil, false, or empty

Divides a number by the specified number.

Makes each character in a string lowercase. It has no effect on strings which are already all lowercase.

HTML encodes the string.

HTML encodes the string without encoding already encoded characters again.

Returns the first item of an array.

Rounds a number down to the nearest whole number. Solid tries to convert the input to a number before the filter is applied.

Join a list of strings returning one String glued by glue

Returns the last item of an array.

Removes all whitespaces (tabs, spaces, and newlines) from the beginning of a string. The filter does not affect spaces between words.

Map through a list of hashes accessing property

Subtracts a number from another number.

Subtracts a number from another number.

Replaces every newline in a string with an HTML line break (<br />).

Adds a number to another number.

Adds the specified string to the beginning of another string.

Removes every occurrence of the specified substring from a string.

Removes only the first occurrence of the specified substring from a string.

Removes only the last occurrence of the specified substring from a string.

Replaces every occurrence of an argument in a string with the second argument.

Replaces only the first occurrence of the first argument in a string with the second argument.

Replaces only the last occurrence of the first argument in a string with the second argument.

Reverses the order of the items in an array. reverse cannot reverse a string.

Rounds an input number to the nearest integer or, if a number is specified as an argument, to that number of decimal places.

Removes all whitespace (tabs, spaces, and newlines) from the right side of a string.

Returns the number of characters in a string or the number of items in an array.

Returns a substring of 1 character beginning at the index specified by the argument passed in. An optional second argument specifies the length of the substring to be returned.

Sorts items in an array by a property of an item in the array. The order of the sorted array is case-sensitive.

Sorts items in an array by a property of an item in the array. The order of the sorted array is case-sensitive.

Split input string into an array of substrings separated by given pattern.

Removes all whitespace (tabs, spaces, and newlines) from both the left and right side of a string. It does not affect spaces between words.

Removes any HTML tags from a string.

Removes any newline characters (line breaks) from a string.

Multiplies a number by another number.

truncate shortens a string down to the number of characters passed as a parameter. If the number of characters specified is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count.

Shortens a string down to the number of words passed as the argument. If the specified number of words is less than the number of words in the string, an ellipsis (…) is appended to the string.

Removes any duplicate elements in an array.

Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.

URL decodes the string.

URL encodes the string.

Creates an array including only the objects with a given property value, or any truthy value by default.

Functions

@spec abs(number() | String.t()) :: number()

Returns the absolute value of a number.

iex> Solid.Filter.abs(-17) 17 iex> Solid.Filter.abs(17) 17 iex> Solid.Filter.abs("-17.5") 17.5

@spec append(any(), any()) :: String.t()

Concatenates two strings and returns the concatenated value.

iex> Solid.Filter.append("www.example.com", "/index.html") "www.example.com/index.html"

Link to this function

apply(filter, args, opts)

View Source

Apply filter if it exists. Otherwise return the first input.

iex> Solid.Filter.apply("upcase", ["ac"], [])

iex> Solid.Filter.apply("no_filter_here", [1, 2, 3], [])

iex> Solid.Filter.apply("no_filter_here", [1, 2, 3], [strict_filters: true]) {:error, %Solid.UndefinedFilterError{filter: "no_filter_here"}, 1}

Link to this function

at_least(input, minimum)

View Source
@spec at_least(number(), number()) :: number()

Limits a number to a minimum value.

iex> Solid.Filter.at_least(5, 3) 5 iex> Solid.Filter.at_least(2, 4) 4

@spec at_most(number(), number()) :: number()

Limits a number to a maximum value.

iex> Solid.Filter.at_most(5, 3) 3 iex> Solid.Filter.at_most(2, 4) 2

@spec base64_decode(iodata()) :: String.t()

Decodes a string in Base64 format.

iex> Solid.Filter.base64_decode("YXBwbGVz") "apples"

@spec base64_encode(iodata()) :: String.t()

Encodes a string to Base64 format.

iex> Solid.Filter.base64_encode("apples") "YXBwbGVz"

Link to this function

base64_url_safe_decode(iodata)

View Source
@spec base64_url_safe_decode(iodata()) :: String.t()

Decodes a string in URL-safe Base64 format.

iex> Solid.Filter.base64_url_safe_decode("YXBwbGVz") "apples"

Link to this function

base64_url_safe_encode(iodata)

View Source
@spec base64_url_safe_encode(iodata()) :: String.t()

Encodes a string to URL-safe Base64 format.

iex> Solid.Filter.base64_url_safe_encode("apples") "YXBwbGVz"

@spec capitalize(any()) :: String.t()

Makes the first character of a string capitalized.

iex> Solid.Filter.capitalize("my great title") "My great title" iex> Solid.Filter.capitalize(1) "1"

@spec ceil(number() | String.t()) :: number()

Rounds the input up to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.

@spec compact(list()) :: list()

Removes all occurrences of nil from a list

iex> Solid.Filter.compact([1, nil, 2, nil, 3]) [1, 2, 3]

Link to this function

compact(input, property)

View Source
@spec concat(list(), list()) :: list()

Concatenates (joins together) multiple arrays. The resulting array contains all the items from the input arrays.

iex> Solid.Filter.concat([1, 2], [3, 4]) [1, 2, 3, 4]

@spec date(DateTime.t() | NaiveDateTime.t() | integer() | String.t(), String.t()) ::
  String.t()

Converts a DateTime/NaiveDateTime struct into another date format. The input may also be a Unix timestamp or an ISO 8601 date string.

The format for this syntax is the same as Calendar.strftime/2.

To get the current time, pass the special word "now" (or "today") to date.

iex> Solid.Filter.date("1970-01-01 00:00:00Z", "%s") "0" iex> Solid.Filter.date("1970-01-01 00:00:01Z", "%s") "1"

@spec default(any(), any()) :: any()

Allows you to specify a fallback in case a value doesn’t exist. default will show its value if the left side is nil, false, or empty

iex> Solid.Filter.default(123, 456) 123

iex> Solid.Filter.default(nil, 456) 456

iex> Solid.Filter.default(false, 456) 456

iex> Solid.Filter.default([], 456) 456

iex> Solid.Filter.default("", 456) 456

Link to this function

divided_by(input, operand)

View Source
@spec divided_by(number(), number()) :: number()

Divides a number by the specified number.

The result is rounded down to the nearest integer (that is, the floor) if the divisor is an integer.

{{ 16 | divided_by: 4 }} iex> Solid.Filter.divided_by(16, 4) 4 iex> Solid.Filter.divided_by(5, 3) 1 iex> Solid.Filter.divided_by(20, 7) 2

@spec downcase(any()) :: String.t()

Makes each character in a string lowercase. It has no effect on strings which are already all lowercase.

iex> Solid.Filter.downcase("aBc") "abc"

iex> Solid.Filter.downcase(456) "456"

iex> Solid.Filter.downcase(nil) ""

@spec escape(iodata()) :: String.t()

HTML encodes the string.

Output iex> Solid.Filter.escape("Have you read 'James & the Giant Peach'?") "Have you read &#39;James &amp; the Giant Peach&#39;?"

@spec escape_once(iodata()) :: String.t()

HTML encodes the string without encoding already encoded characters again.

This mimics the regex based approach of the ruby library.

Output "1 &lt; 2 &amp; 3"

iex> Solid.Filter.escape_once("1 &lt; 2 &amp; 3") "1 &lt; 2 &amp; 3"

@spec first(list()) :: any()

Returns the first item of an array.

iex> Solid.Filter.first([1, 2, 3]) 1 iex> Solid.Filter.first([]) nil

@spec floor(number() | String.t()) :: integer()

Rounds a number down to the nearest whole number. Solid tries to convert the input to a number before the filter is applied.

iex> Solid.Filter.floor(1.2) 1 iex> Solid.Filter.floor(2.0) 2 iex> Solid.Filter.floor("3.5") 3

Link to this function

join(input, glue \\ " ")

View Source
@spec join(list(), String.t()) :: String.t()

Join a list of strings returning one String glued by glue

iex> Solid.Filter.join(["a", "b", "c"]) "a b c" iex> Solid.Filter.join(["a", "b", "c"], "-") "a-b-c"

@spec last(list()) :: any()

Returns the last item of an array.

iex> Solid.Filter.last([1, 2, 3]) 3 iex> Solid.Filter.last([]) nil

@spec lstrip(String.t()) :: String.t()

Removes all whitespaces (tabs, spaces, and newlines) from the beginning of a string. The filter does not affect spaces between words.

iex> Solid.Filter.lstrip(" So much room for activities! ") "So much room for activities! "

Map through a list of hashes accessing property

iex> Solid.Filter.map([%{"a" => "A"}, %{"a" => 1}], "a") ["A", 1]

@spec minus(number(), number()) :: number()

Subtracts a number from another number.

iex> Solid.Filter.minus(4, 2) 2 iex> Solid.Filter.minus(16, 4) 12 iex> Solid.Filter.minus(183.357, 12) 171.357

Link to this function

modulo(dividend, divisor)

View Source
@spec modulo(number(), number()) :: number()

Subtracts a number from another number.

iex> Solid.Filter.modulo(3, 2) 1 iex> Solid.Filter.modulo(24, 7) 3 iex> Solid.Filter.modulo(183.357, 12) 3.357

@spec newline_to_br(iodata()) :: String.t()

Replaces every newline in a string with an HTML line break (<br />).

Output iex> Solid.Filter.newline_to_br("Test \ntext\r\n with line breaks.") "Test <br />\ntext<br />\r\n with line breaks."

iex> Solid.Filter.newline_to_br([[["Test \ntext\r\n with "] | "line breaks."]]) "Test <br />\ntext<br />\r\n with line breaks."

@spec plus(number(), number()) :: number()

Adds a number to another number.

iex> Solid.Filter.plus(4, 2) 6 iex> Solid.Filter.plus(16, 4) 20 iex> Solid.Filter.plus("16", 4) 20 iex> Solid.Filter.plus(183.357, 12) 195.357 iex> Solid.Filter.plus("183.357", 12) 195.357 iex> Solid.Filter.plus("183.ABC357", 12) nil

@spec prepend(any(), any()) :: String.t()

Adds the specified string to the beginning of another string.

iex> Solid.Filter.prepend("/index.html", "www.example.com") "www.example.com/index.html"

@spec remove(String.t(), String.t()) :: String.t()

Removes every occurrence of the specified substring from a string.

iex> Solid.Filter.remove("I strained to see the train through the rain", "rain") "I sted to see the t through the "

Link to this function

remove_first(input, string)

View Source
@spec remove_first(String.t(), String.t()) :: String.t()

Removes only the first occurrence of the specified substring from a string.

iex> Solid.Filter.remove_first("I strained to see the train through the rain", "rain") "I sted to see the train through the rain"

Link to this function

remove_last(input, string)

View Source
@spec remove_last(String.t(), String.t()) :: String.t()

Removes only the last occurrence of the specified substring from a string.

iex> Solid.Filter.remove_last("I strained to see the train through the rain", "rain") "I strained to see the train through the "

Link to this function

replace(input, string, replacement \\ "")

View Source
@spec replace(String.t(), String.t(), String.t()) :: String.t()

Replaces every occurrence of an argument in a string with the second argument.

iex> Solid.Filter.replace("Take my protein pills and put my helmet on", "my", "your") "Take your protein pills and put your helmet on"

Link to this function

replace_first(input, string, replacement \\ "")

View Source
@spec replace_first(String.t(), String.t(), String.t()) :: String.t()

Replaces only the first occurrence of the first argument in a string with the second argument.

iex> Solid.Filter.replace_first("Take my protein pills and put my helmet on", "my", "your") "Take your protein pills and put my helmet on"

Link to this function

replace_last(input, string, replacement \\ "")

View Source
@spec replace_last(String.t(), String.t(), String.t()) :: String.t()

Replaces only the last occurrence of the first argument in a string with the second argument.

iex> Solid.Filter.replace_last("Take my protein pills and put my helmet on", "my", "your") "Take my protein pills and put your helmet on"

@spec reverse(list()) :: list()

Reverses the order of the items in an array. reverse cannot reverse a string.

iex> Solid.Filter.reverse(["a", "b", "c"]) ["c", "b", "a"]

Link to this function

round(input, precision \\ nil)

View Source

Rounds an input number to the nearest integer or, if a number is specified as an argument, to that number of decimal places.

iex> Solid.Filter.round(1.2) 1 iex> Solid.Filter.round(2.7) 3 iex> Solid.Filter.round(183.357, 2) 183.36

@spec rstrip(String.t()) :: String.t()

Removes all whitespace (tabs, spaces, and newlines) from the right side of a string.

iex> Solid.Filter.rstrip(" So much room for activities! ") " So much room for activities!"

@spec size(String.t() | list()) :: non_neg_integer()

Returns the number of characters in a string or the number of items in an array.

iex> Solid.Filter.size("Ground control to Major Tom.") 28 iex> Solid.Filter.size(~w(ground control to Major Tom.)) 5

Link to this function

slice(input, offset, length \\ nil)

View Source
@spec slice(String.t(), integer(), non_neg_integer() | nil) :: String.t()

Returns a substring of 1 character beginning at the index specified by the argument passed in. An optional second argument specifies the length of the substring to be returned.

String indices are numbered starting from 0.

iex> Solid.Filter.slice("Liquid", 0) "L"

iex> Solid.Filter.slice("Liquid", 2) "q"

iex> Solid.Filter.slice("Liquid", 2, 5) "quid" iex> Solid.Filter.slice("Liquid", -3, 2) "ui"

@spec sort(list()) :: list()

Sorts items in an array by a property of an item in the array. The order of the sorted array is case-sensitive.

iex> Solid.Filter.sort(~w(zebra octopus giraffe SallySnake)) ~w(SallySnake giraffe octopus zebra)

@spec sort_natural(list()) :: list()

Sorts items in an array by a property of an item in the array. The order of the sorted array is case-sensitive.

iex> Solid.Filter.sort_natural(~w(zebra octopus giraffe SallySnake)) ~w(giraffe octopus SallySnake zebra)

@spec split(any(), String.t()) :: [String.t()]

Split input string into an array of substrings separated by given pattern.

iex> Solid.Filter.split("a b c", " ") ~w(a b c) iex> Solid.Filter.split("", " ") [""]

@spec strip(String.t()) :: String.t()

Removes all whitespace (tabs, spaces, and newlines) from both the left and right side of a string. It does not affect spaces between words.

iex> Solid.Filter.strip(" So much room for activities! ") "So much room for activities!"

@spec strip_html(iodata()) :: String.t()

Removes any HTML tags from a string.

This mimics the regex based approach of the ruby library.

Output iex> Solid.Filter.strip_html("Have <em>you</em> read <strong>Ulysses</strong>?") "Have you read Ulysses?"

@spec strip_newlines(iodata()) :: String.t()

Removes any newline characters (line breaks) from a string.

Output iex> Solid.Filter.strip_newlines("Test \ntext\r\n with line breaks.") "Test text with line breaks."

iex> Solid.Filter.strip_newlines([[["Test \ntext\r\n with "] | "line breaks."]]) "Test text with line breaks."

@spec times(number(), number()) :: number()

Multiplies a number by another number.

iex> Solid.Filter.times(3, 2) 6 iex> Solid.Filter.times(24, 7) 168 iex> Solid.Filter.times(183.357, 12) 2200.284

Link to this function

truncate(input, length, ellipsis \\ "...")

View Source
@spec truncate(String.t(), non_neg_integer(), String.t()) :: String.t()

truncate shortens a string down to the number of characters passed as a parameter. If the number of characters specified is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count.

iex> Solid.Filter.truncate("Ground control to Major Tom.", 20) "Ground control to..."

Custom ellipsis

truncate takes an optional second parameter that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (…), but you can specify a different sequence.

The length of the second parameter counts against the number of characters specified by the first parameter. For example, if you want to truncate a string to exactly 10 characters, and use a 3-character ellipsis, use 13 for the first parameter of truncate, since the ellipsis counts as 3 characters.

iex> Solid.Filter.truncate("Ground control to Major Tom.", 25, ", and so on") "Ground control, and so on"

No ellipsis

You can truncate to the exact number of characters specified by the first parameter and show no trailing characters by passing a blank string as the second parameter:

iex> Solid.Filter.truncate("Ground control to Major Tom.", 20, "") "Ground control to Ma"

Link to this function

truncatewords(input, max_words, ellipsis \\ "...")

View Source
@spec truncatewords(nil | String.t(), non_neg_integer(), String.t()) :: String.t()

Shortens a string down to the number of words passed as the argument. If the specified number of words is less than the number of words in the string, an ellipsis (…) is appended to the string.

iex> Solid.Filter.truncatewords("Ground control to Major Tom.", 3) "Ground control to..."

Custom ellipsis

truncatewords takes an optional second parameter that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (…), but you can specify a different sequence.

iex> Solid.Filter.truncatewords("Ground control to Major Tom.", 3, "--") "Ground control to--"

No ellipsis

You can avoid showing trailing characters by passing a blank string as the second parameter:

iex> Solid.Filter.truncatewords("Ground control to Major Tom.", 3, "") "Ground control to"

@spec uniq(list()) :: list()

Removes any duplicate elements in an array.

Output iex> Solid.Filter.uniq(~w(ants bugs bees bugs ants)) ~w(ants bugs bees)

@spec upcase(any()) :: String.t()

Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.

iex> Solid.Filter.upcase("aBc") "ABC"

iex> Solid.Filter.upcase(456) "456"

iex> Solid.Filter.upcase(nil) ""

URL decodes the string.

Output iex> Solid.Filter.url_decode("%27Stop%21%27+said+Fred") "'Stop!' said Fred"

URL encodes the string.

Output iex> Solid.Filter.url_encode("john@liquid.com") "john%40liquid.com"

iex> Solid.Filter.url_encode("Tetsuro Takara") "Tetsuro+Takara"

@spec where(list(), String.t()) :: list()
Link to this function

where(input, key, value)

View Source
@spec where(list(), String.t(), String.t()) :: list()

Creates an array including only the objects with a given property value, or any truthy value by default.

Output iex> input = [ ...> %{"id" => 1, "type" => "kitchen"}, ...> %{"id" => 2, "type" => "bath"}, ...> %{"id" => 3, "type" => "kitchen"} ...> ] iex> Solid.Filter.where(input, "type", "kitchen") [%{"id" => 1, "type" => "kitchen"}, %{"id" => 3, "type" => "kitchen"}]

iex> input = [ ...> %{"id" => 1, "available" => true}, ...> %{"id" => 2, "type" => false}, ...> %{"id" => 3, "available" => true} ...> ] iex> Solid.Filter.where(input, "available") [%{"id" => 1, "available" => true}, %{"id" => 3, "available" => true}]