Contains all the basic filters for Liquid
Summary
Functions
Returns the absolute value of value.
Appends text to the end of value
Sets a minimum value
Sets a maximum value
Decodes a base 64 string.
Encodes a string into base 64.
Decodes a URL-safe base 64 string.
Encodes a string into URL-safe base 64.
Capitalizes a string
Rounds value up to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
Removes any nil values from an array.
Removes any items in the array whose property is nil.
Concatenates (joins together) multiple arrays. The resulting array contains all the items
Converts value timestamp into another date format.
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.
Suggest a similar filter name for name given the candidates from module.
Returns " (did you meanfoo?)" when a close match exists, otherwise "".
Divides a number by another number.
Makes each character in a string lowercase. It has no effect on strings which are already all lowercase.
Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn’t change strings that don’t have anything to escape.
Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn’t change strings that don’t have anything to escape.
Return the list of user-callable filter names exported by module (and the
default filter module if different).
Returns the first item in an array whose property is truthy. Returns nil
if no item matches or the array is empty.
Returns the index of the first item in an array whose property is truthy or
matches target_value. Returns nil if no item matches or the array is empty.
Returns the first item of an array.
Rounds the input down to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
Returns true if any item in the array has the given property truthy or
matching target_value, otherwise false. Returns false for empty arrays.
Combines the items in values into a single string using joiner as a separator.
Returns the last item of arr.
Removes all whitespace (tabs, spaces, and newlines) from the left side of a string. It does not affect spaces between words.
Creates an array (arr) of values by extracting the values of a named property from another object (key).
Subtracts a number from another number.
Returns the remainder of a division operation.
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.
Returns the items of an array whose property is falsy, or whose property
is not equal to target_value when given.
Removes every occurrence of the specified substring from a string.
Removes every occurrence of the specified substring from a string.
Removes only the last occurrence of the specified substring from a string.
Replaces every occurrence of the first 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. Returns the input unchanged if no occurrence is found.
Reverses the order of the items in an array. reverse cannot reverse a string.
Rounds a number to the nearest integer or, if a number is passed as an argument, to that number of decimal places.
Removes all whitespace (tabs, spaces, and newlines) from the right side of a string. It does not affect spaces between words.
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 first argument. An optional second argument specifies the length of the substring to be returned.
Sorts items in an array in case-sensitive order.
Sorts items in an array in case-insensitive order.
Divides a string into an array using the argument as a separator. split is commonly used to convert comma-separated items from a string to an array.
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. The contents of <script>, <style>,
and HTML comments are stripped along with the tags themselves, matching the
Liquid gem's strip_html behavior.
Removes any newline characters (line breaks) from a string.
Returns the sum of the items in an array. Items are coerced to numbers; with
a property argument, the named property of each item is summed instead.
Multiplies a number by another number.
Shortens a string down to the number of characters passed as an argument. If the specified number of characters 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 characters passed as an argument. If the specified number of characters is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count.
Removes any duplicate elements in an array.
Removes items from an array that share a value at the given property.
Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.
Decodes a string that has been encoded as a URL or by url_encode/2.
Decodes a string that has been encoded as a URL or by url_encode/2.
Creates an array including only the objects with a given truthy property value
Creates an array including only the objects with a given property value, or any truthy value by default.
Types
Callbacks
Functions
Returns the absolute value of value.
Examples
iex> Liquex.Filter.abs(-1, %{})
1
iex> Liquex.Filter.abs(1, %{})
1
iex> Liquex.Filter.abs("-1.1", %{})
1.1
Appends text to the end of value
Examples
iex> Liquex.Filter.append("myfile", ".html", %{})
"myfile.html"
Sets a minimum value
Examples
iex> Liquex.Filter.at_least(3, 5, %{})
5
iex> Liquex.Filter.at_least(5, 3, %{})
5
iex> Liquex.Filter.at_least("5", "3", %{})
5
Sets a maximum value
Examples
iex> Liquex.Filter.at_most(4, 5, %{})
4
iex> Liquex.Filter.at_most(4, 3, %{})
3
iex> Liquex.Filter.at_most("4", "3", %{})
3
Decodes a base 64 string.
Examples
iex> Liquex.Filter.base64_decode("X2hlbGxvXw==", %{})
"_hello_"
Encodes a string into base 64.
Examples
iex> Liquex.Filter.base64_encode("_hello_", %{})
"X2hlbGxvXw=="
Decodes a URL-safe base 64 string.
Examples
iex> Liquex.Filter.base64_url_safe_decode("X2hlbGxvIHdvcmxkPysv", %{})
"_hello world?+/"
Encodes a string into URL-safe base 64.
Examples
iex> Liquex.Filter.base64_url_safe_encode("_hello world?+/", %{})
"X2hlbGxvIHdvcmxkPysv"
Capitalizes a string
Examples
iex> Liquex.Filter.capitalize("title", %{})
"Title"
iex> Liquex.Filter.capitalize("my great title", %{})
"My great title"
Rounds value up to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
Examples
iex> Liquex.Filter.ceil(1.2, %{})
2
iex> Liquex.Filter.ceil(2.0, %{})
2
iex> Liquex.Filter.ceil(183.357, %{})
184
iex> Liquex.Filter.ceil("3.5", %{})
4
Removes any nil values from an array.
Examples
iex> Liquex.Filter.compact([1, 2, nil, 3], %{})
[1,2,3]
iex> Liquex.Filter.compact([1, 2, 3], %{})
[1,2,3]
Removes any items in the array whose property is nil.
Examples
iex> Liquex.Filter.compact([%{"a" => 1}, %{"a" => nil}, %{"a" => 2}], "a", %{})
[%{"a" => 1}, %{"a" => 2}]
Concatenates (joins together) multiple arrays. The resulting array contains all the items
Examples
iex> Liquex.Filter.concat([1,2], [3,4], %{})
[1,2,3,4]
Converts value timestamp into another date format.
The format for this syntax is the same as strftime. The input uses the same format as Ruby’s Time.parse.
Examples
iex> Liquex.Filter.date(~D[2000-01-01], "%m/%d/%Y", %{})
"01/01/2000"
iex> Liquex.Filter.date("2000-01-01", "%m/%d/%Y", %{})
"01/01/2000"
iex> Liquex.Filter.date("January 1, 2000", "%m/%d/%Y", %{})
"01/01/2000"
iex> Liquex.Filter.date("1/2/2000", "%m/%d/%Y", %{})
"01/02/2000"
iex> Liquex.Filter.date("March 14, 2016", "%b %d, %y", %{})
"Mar 14, 16"
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.
Examples
iex> Liquex.Filter.default("1.99", "2.99", %{})
"1.99"
iex> Liquex.Filter.default("", "2.99", %{})
"2.99"
iex> Liquex.Filter.default(false, "x", [{"allow_false", true}], %{})
false
iex> Liquex.Filter.default(false, "x", [{"allow_false", false}], %{})
"x"
Suggest a similar filter name for name given the candidates from module.
Returns " (did you meanfoo?)" when a close match exists, otherwise "".
Divides a number by another number.
Examples
The result is rounded down to the nearest integer (that is, the floor) if the divisor is an integer.
iex> Liquex.Filter.divided_by(16, 4, %{})
4
iex> Liquex.Filter.divided_by(5, 3, %{})
1
iex> Liquex.Filter.divided_by(20, 5, %{})
4
iex> Decimal.equal?(Liquex.Filter.divided_by(20, 7.0, %{}), Decimal.new("2.857142857142857142857142857"))
true
Makes each character in a string lowercase. It has no effect on strings which are already all lowercase.
Examples
iex> Liquex.Filter.downcase("Parker Moore", %{})
"parker moore"
iex> Liquex.Filter.downcase("apple", %{})
"apple"
Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn’t change strings that don’t have anything to escape.
Examples
iex> Liquex.Filter.escape("Have you read 'James & the Giant Peach'?", %{})
"Have you read 'James & the Giant Peach'?"
iex> Liquex.Filter.escape("Tetsuro Takara", %{})
"Tetsuro Takara"
Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn’t change strings that don’t have anything to escape.
Examples
iex> Liquex.Filter.escape_once("1 < 2 & 3", %{})
"1 < 2 & 3"
Return the list of user-callable filter names exported by module (and the
default filter module if different).
Returns the first item in an array whose property is truthy. Returns nil
if no item matches or the array is empty.
Examples
iex> Liquex.Filter.find([%{"v" => 1}, %{"v" => 2}], "v", 2, %{})
%{"v" => 2}
iex> Liquex.Filter.find([%{"v" => 1}, %{"v" => 2}], "v", %{})
%{"v" => 1}
iex> Liquex.Filter.find([], "v", 2, %{})
nil
Returns the index of the first item in an array whose property is truthy or
matches target_value. Returns nil if no item matches or the array is empty.
Examples
iex> Liquex.Filter.find_index([%{"v" => 1}, %{"v" => 2}], "v", 2, %{})
1
iex> Liquex.Filter.find_index([%{"v" => 1}, %{"v" => 2}], "v", %{})
0
iex> Liquex.Filter.find_index([], "v", 2, %{})
nil
Returns the first item of an array.
Examples
iex> Liquex.Filter.first([1, 2, 3], %{})
1
iex> Liquex.Filter.first([], %{})
nil
Rounds the input down to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
Examples
iex> Liquex.Filter.floor(1.2, %{})
1
iex> Liquex.Filter.floor(2.0, %{})
2
Returns true if any item in the array has the given property truthy or
matching target_value, otherwise false. Returns false for empty arrays.
Examples
iex> Liquex.Filter.has([%{"v" => 1}, %{"v" => 2}], "v", 2, %{})
true
iex> Liquex.Filter.has([%{"v" => 1}], "v", 99, %{})
false
iex> Liquex.Filter.has([], "v", 2, %{})
false
Combines the items in values into a single string using joiner as a separator.
Examples
iex> Liquex.Filter.join(~w(John Paul George Ringo), " and ", %{})
"John and Paul and George and Ringo"
@spec last(any(), Liquex.Context.t()) :: any()
Returns the last item of arr.
Examples
iex> Liquex.Filter.last([1, 2, 3], %{})
3
iex> Liquex.Filter.first([], %{})
nil
@spec lstrip(String.t(), Liquex.Context.t()) :: String.t()
Removes all whitespace (tabs, spaces, and newlines) from the left side of a string. It does not affect spaces between words.
Examples
iex> Liquex.Filter.lstrip(" So much room for activities! ", %{})
"So much room for activities! "
@spec map([any()] | nil, term(), Liquex.Context.t()) :: [any()]
Creates an array (arr) of values by extracting the values of a named property from another object (key).
Examples
iex> Liquex.Filter.map([%{"a" => 1}, %{"a" => 2, "b" => 1}], "a", %{})
[1, 2]
@spec minus(number() | nil, number() | nil, Liquex.Context.t()) :: number()
Subtracts a number from another number.
Examples
iex> Liquex.Filter.minus(4, 2, %{})
2
iex> Liquex.Filter.minus(183, 12, %{})
171
iex> Decimal.equal?(Liquex.Filter.minus(183.357, 12, %{}), Decimal.new("171.357"))
true
@spec modulo(number() | nil, number() | nil, Liquex.Context.t()) :: number()
Returns the remainder of a division operation.
Examples
iex> Liquex.Filter.modulo(3, 2, %{})
1
iex> Liquex.Filter.modulo(183, 12, %{})
3
iex> Decimal.equal?(Liquex.Filter.modulo(10.5, 3, %{}), Decimal.new("1.5"))
true
@spec newline_to_br(String.t(), Liquex.Context.t()) :: String.t()
Replaces every newline ( ) in a string with an HTML line break (<br />).
Examples
iex> Liquex.Filter.newline_to_br("\nHello\nthere\n", %{})
"<br />\nHello<br />\nthere<br />\n"
@spec plus(number() | nil, number() | nil, Liquex.Context.t()) :: number()
Adds a number to another number.
Examples
iex> Liquex.Filter.plus(4, 2, %{})
6
iex> Liquex.Filter.plus(183, 12, %{})
195
iex> Decimal.equal?(Liquex.Filter.plus(0.1, 0.2, %{}), Decimal.new("0.3"))
true
iex> Decimal.equal?(Liquex.Filter.plus(9.99, 14.5, %{}), Decimal.new("24.49"))
true
Adds the specified string to the beginning of another string.
Examples
iex> Liquex.Filter.prepend("apples, oranges, and bananas", "Some fruit: ", %{})
"Some fruit: apples, oranges, and bananas"
iex> Liquex.Filter.prepend("/index.html", "example.com", %{})
"example.com/index.html"
Returns the items of an array whose property is falsy, or whose property
is not equal to target_value when given.
Examples
iex> Liquex.Filter.reject([%{"v" => 1}, %{"v" => 2}, %{"v" => 3}], "v", 2, %{})
[%{"v" => 1}, %{"v" => 3}]
iex> Liquex.Filter.reject([%{"a" => true}, %{"a" => false}], "a", %{})
[%{"a" => false}]
Removes every occurrence of the specified substring from a string.
Examples
iex> Liquex.Filter.remove("I strained to see the train through the rain", "rain", %{})
"I sted to see the t through the "
Removes every occurrence of the specified substring from a string.
Examples
iex> Liquex.Filter.remove_first("I strained to see the train through the rain", "rain", %{})
"I sted to see the train through the rain"
Removes only the last occurrence of the specified substring from a string.
Examples
iex> Liquex.Filter.remove_last("I strained to see the train through the rain", "rain", %{})
"I strained to see the train through the "
Replaces every occurrence of the first argument in a string with the second argument.
Examples
iex> Liquex.Filter.replace("Take my protein pills and put my helmet on", "my", "your", %{})
"Take your protein pills and put your helmet on"
Replaces only the first occurrence of the first argument in a string with the second argument.
Examples
iex> Liquex.Filter.replace_first("Take my protein pills and put my helmet on", "my", "your", %{})
"Take your protein pills and put my helmet on"
Replaces only the last occurrence of the first argument in a string with the second argument. Returns the input unchanged if no occurrence is found.
Examples
iex> Liquex.Filter.replace_last("red, red, red", "red", "blue", %{})
"red, red, blue"
iex> Liquex.Filter.replace_last("abc", "x", "y", %{})
"abc"
Reverses the order of the items in an array. reverse cannot reverse a string.
Examples
iex> Liquex.Filter.reverse(~w(apples oranges peaches plums), %{})
["plums", "peaches", "oranges", "apples"]
Rounds a number to the nearest integer or, if a number is passed as an argument, to that number of decimal places.
Examples
iex> Liquex.Filter.round(1, %{})
1
iex> Liquex.Filter.round(1.2, %{})
1
iex> Liquex.Filter.round(2.7, %{})
3
iex> Liquex.Filter.round(183.357, 2, %{})
183.36
iex> Liquex.Filter.round(183.357, "invalid", %{})
183
iex> Liquex.Filter.round(183.357, 0, %{})
183
iex> Liquex.Filter.round(1234, -2, %{})
1200
iex> Liquex.Filter.round(183.357, -1, %{})
180
Removes all whitespace (tabs, spaces, and newlines) from the right side of a string. It does not affect spaces between words.
Examples
iex> Liquex.Filter.rstrip(" So much room for activities! ", %{})
" So much room for activities!"
Returns the number of characters in a string or the number of items in an array.
Examples
iex> Liquex.Filter.size("Ground control to Major Tom.", %{})
28
iex> Liquex.Filter.size(~w(apples oranges peaches plums), %{})
4
Returns a substring of 1 character beginning at the index specified by the first argument. An optional second argument specifies the length of the substring to be returned.
Examples
iex> Liquex.Filter.slice("Liquid", 0, %{})
"L"
iex> Liquex.Filter.slice("Liquid", 2, %{})
"q"
iex> Liquex.Filter.slice("Liquid", 2, 5, %{})
"quid"If the first argument is a negative number, the indices are counted from the end of the string:
Examples
iex> Liquex.Filter.slice("Liquid", -3, 2, %{})
"ui"Slicing an array returns a sub-array.
Examples
iex> Liquex.Filter.slice([1, 2, 3, 4, 5], 1, 2, %{})
[2, 3]
iex> Liquex.Filter.slice([1, 2, 3, 4, 5], 2, %{})
[3]
Sorts items in an array in case-sensitive order.
Examples
iex> Liquex.Filter.sort(["zebra", "octopus", "giraffe", "Sally Snake"], %{})
["Sally Snake", "giraffe", "octopus", "zebra"]
Sorts items in an array in case-insensitive order.
Examples
iex> Liquex.Filter.sort_natural(["zebra", "octopus", "giraffe", "Sally Snake"], %{})
["giraffe", "octopus", "Sally Snake", "zebra"]
Divides a string into an array using the argument as a separator. split is commonly used to convert comma-separated items from a string to an array.
Examples
iex> Liquex.Filter.split("John, Paul, George, Ringo", ", ", %{})
["John", "Paul", "George", "Ringo"]
Removes all whitespace (tabs, spaces, and newlines) from both the left and right side of a string. It does not affect spaces between words.
Examples
iex> Liquex.Filter.strip(" So much room for activities! ", %{})
"So much room for activities!"
Removes any HTML tags from a string. The contents of <script>, <style>,
and HTML comments are stripped along with the tags themselves, matching the
Liquid gem's strip_html behavior.
Examples
iex> Liquex.Filter.strip_html("Have <em>you</em> read <strong>Ulysses</strong>?", %{})
"Have you read Ulysses?"
iex> Liquex.Filter.strip_html("<script>alert(1)</script>hi", %{})
"hi"
iex> Liquex.Filter.strip_html("<style>p {}</style>hi", %{})
"hi"
iex> Liquex.Filter.strip_html("a<!-- gone -->b", %{})
"ab"
Removes any newline characters (line breaks) from a string.
Examples
iex> Liquex.Filter.strip_newlines("Hello\nthere", %{})
"Hellothere"
Returns the sum of the items in an array. Items are coerced to numbers; with
a property argument, the named property of each item is summed instead.
Examples
iex> Liquex.Filter.sum([1, 2, 3], %{})
6
iex> Liquex.Filter.sum([%{"v" => 1}, %{"v" => 2}], "v", %{})
3
iex> Liquex.Filter.sum([], %{})
0
Multiplies a number by another number.
Examples
iex> Liquex.Filter.times(3, 4, %{})
12
iex> Liquex.Filter.times(24, 7, %{})
168
iex> Liquex.Filter.times(7, 12, %{})
84
iex> Decimal.equal?(Liquex.Filter.times(9.99, 2, %{}), Decimal.new("19.98"))
true
Shortens a string down to the number of characters passed as an argument. If the specified number of characters is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count.
Examples
iex> Liquex.Filter.truncate("Ground control to Major Tom.", 20, %{})
"Ground control to..."
iex> Liquex.Filter.truncate("Ground control to Major Tom.", 25, ", and so on", %{})
"Ground control, and so on"
iex> Liquex.Filter.truncate("Ground control to Major Tom.", 20, "", %{})
"Ground control to Ma"
Shortens a string down to the number of characters passed as an argument. If the specified number of characters is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count.
Examples
iex> Liquex.Filter.truncatewords("Ground control to Major Tom.", 3, %{})
"Ground control to..."
iex> Liquex.Filter.truncatewords("Ground control to Major Tom.", 3, "--", %{})
"Ground control to--"
iex> Liquex.Filter.truncatewords("Ground control to Major Tom.", 3, "", %{})
"Ground control to"
iex> Liquex.Filter.truncatewords("a b c d e", 0, %{})
"a..."
Removes any duplicate elements in an array.
Examples
iex> Liquex.Filter.uniq(~w(ants bugs bees bugs ants), %{})
["ants", "bugs", "bees"]
Removes items from an array that share a value at the given property.
Examples
iex> Liquex.Filter.uniq([%{"id" => 1}, %{"id" => 2}, %{"id" => 1}], "id", %{})
[%{"id" => 1}, %{"id" => 2}]
Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.
Examples
iex> Liquex.Filter.upcase("Parker Moore", %{})
"PARKER MOORE"
iex> Liquex.Filter.upcase("APPLE", %{})
"APPLE"
Decodes a string that has been encoded as a URL or by url_encode/2.
Examples
iex> Liquex.Filter.url_decode("%27Stop%21%27+said+Fred", %{})
"'Stop!' said Fred"
Decodes a string that has been encoded as a URL or by url_encode/2.
Examples
iex> Liquex.Filter.url_encode("john@liquid.com", %{})
"john%40liquid.com"
iex> Liquex.Filter.url_encode("Tetsuro Takara", %{})
"Tetsuro+Takara"
Creates an array including only the objects with a given truthy property value
Examples
iex> Liquex.Filter.where([%{"b" => true, "value" => 1}, %{"b" => 1, "value" => 2}, %{"b" => false, "value" => 3}], "b", %{})
[%{"b" => true, "value" => 1}, %{"b" => 1, "value" => 2}]
Creates an array including only the objects with a given property value, or any truthy value by default.
Examples
iex> Liquex.Filter.where([%{"b" => 2}, %{"b" => 1}], "b", 1, %{})
[%{"b" => 1}]