Solid.Filter (solid v0.15.2) View Source
Standard filters
Link to this section 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.
Link to this section Functions
Specs
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
Specs
Concatenates two strings and returns the concatenated value.
iex> Solid.Filter.append("www.example.com", "/index.html") "www.example.com/index.html"
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}
Specs
Limits a number to a minimum value.
iex> Solid.Filter.at_least(5, 3) 5 iex> Solid.Filter.at_least(2, 4) 4
Specs
Limits a number to a maximum value.
iex> Solid.Filter.at_most(5, 3) 3 iex> Solid.Filter.at_most(2, 4) 2
Specs
Decodes a string in Base64 format.
iex> Solid.Filter.base64_decode("YXBwbGVz") "apples"
Specs
Encodes a string to Base64 format.
iex> Solid.Filter.base64_encode("apples") "YXBwbGVz"
Specs
Decodes a string in URL-safe Base64 format.
iex> Solid.Filter.base64_url_safe_decode("YXBwbGVz") "apples"
Specs
Encodes a string to URL-safe Base64 format.
iex> Solid.Filter.base64_url_safe_encode("apples") "YXBwbGVz"
Specs
Makes the first character of a string capitalized.
iex> Solid.Filter.capitalize("my great title") "My great title" iex> Solid.Filter.capitalize(1) "1"
Specs
Rounds the input up to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
Specs
Removes all occurrences of nil from a list
iex> Solid.Filter.compact([1, nil, 2, nil, 3]) [1, 2, 3]
Specs
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]
Specs
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
.
Specs
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
Specs
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
Specs
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) ""
Specs
HTML encodes the string.
Output iex> Solid.Filter.escape("Have you read 'James & the Giant Peach'?") "Have you read 'James & the Giant Peach'?"
Specs
HTML encodes the string without encoding already encoded characters again.
This mimics the regex based approach of the ruby library.
Output "1 < 2 & 3"
iex> Solid.Filter.escape_once("1 < 2 & 3") "1 < 2 & 3"
Specs
Returns the first item of an array.
iex> Solid.Filter.first([1, 2, 3]) 1 iex> Solid.Filter.first([]) nil
Specs
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
Specs
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"
Specs
Returns the last item of an array.
iex> Solid.Filter.last([1, 2, 3]) 3 iex> Solid.Filter.last([]) nil
Specs
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]
Specs
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
Specs
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
Specs
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."
Specs
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
Specs
Adds the specified string to the beginning of another string.
iex> Solid.Filter.prepend("/index.html", "www.example.com") "www.example.com/index.html"
Specs
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 "
Specs
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"
Specs
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 "
Specs
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"
Specs
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"
Specs
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"
Specs
Reverses the order of the items in an array. reverse cannot reverse a string.
iex> Solid.Filter.reverse(["a", "b", "c"]) ["c", "b", "a"]
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
Specs
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!"
Specs
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
Specs
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"
Specs
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)
Specs
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)
Specs
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("", " ") [""]
Specs
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!"
Specs
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?"
Specs
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."
Specs
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
Specs
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"
Specs
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"
Specs
Removes any duplicate elements in an array.
Output iex> Solid.Filter.uniq(~w(ants bugs bees bugs ants)) ~w(ants bugs bees)
Specs
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"
Specs
Specs
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}]