Solid.StandardFilter (solid v1.0.1)

View Source

Standard filters

Summary

Functions

Returns the absolute value of a number.

Concatenates two strings and returns the concatenated value.

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.

Returns sum of all elements in an enumerable Allowing for an optional property to be passed in

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

abs(input)

@spec abs(term()) :: String.t()

Returns the absolute value of a number.

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

append(input, string)

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

Concatenates two strings and returns the concatenated value.

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

apply(filter, args, loc, opts)

@spec apply(String.t(), list(), Solid.Parser.Loc.t(), keyword()) ::
  {:ok, any()} | {:error, Exception.t(), any()} | {:error, Exception.t()}

at_least(input, minimum)

@spec at_least(term(), term()) :: String.t()

Limits a number to a minimum value.

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

at_most(input, maximum)

@spec at_most(term(), term()) :: String.t()

Limits a number to a maximum value.

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

base64_decode(input)

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

Decodes a string in Base64 format.

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

base64_encode(input)

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

Encodes a string to Base64 format.

iex> Solid.StandardFilter.base64_encode("apples") "YXBwbGVz" iex> Solid.StandardFilter.base64_encode(3) "Mw=="

base64_url_safe_decode(iodata)

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

Decodes a string in URL-safe Base64 format.

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

base64_url_safe_encode(input)

@spec base64_url_safe_encode(term()) :: String.t()

Encodes a string to URL-safe Base64 format.

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

iex> Solid.StandardFilter.base64_url_safe_encode(3) "Mw=="

capitalize(input)

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

Makes the first character of a string capitalized.

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

ceil(input)

@spec ceil(term()) :: String.t()

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

compact(input)

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

Removes all occurrences of nil from a list

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

compact(input, property)

concat(input, list)

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

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

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

iex> Solid.StandardFilter.concat(nil, [3, 4]) [3, 4]

date(date, format)

@spec date(term(), term()) :: 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.StandardFilter.date("1970-01-01 00:00:00Z", "%s") "0" iex> Solid.StandardFilter.date("1970-01-01 00:00:01Z", "%s") "1"

default(input, value \\ "", opts \\ %{})

@spec default(any(), any(), map()) :: 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.StandardFilter.default(123, 456) 123

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

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

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

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

divided_by(input, operand)

@spec divided_by(term(), term()) :: String.t()

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.StandardFilter.divided_by(16, 4) "4" iex> Solid.StandardFilter.divided_by(5, 3) "1" iex> Solid.StandardFilter.divided_by(20, 7) "2"

downcase(input)

@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.StandardFilter.downcase("aBc") "abc"

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

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

escape(iodata)

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

HTML encodes the string.

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

escape_once(iodata)

@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.StandardFilter.escape_once("1 &lt; 2 &amp; 3") "1 &lt; 2 &amp; 3"

first(input)

@spec first(term()) :: any()

Returns the first item of an array.

iex> Solid.StandardFilter.first([1, 2, 3]) 1 iex> Solid.StandardFilter.first([]) nil iex> Solid.StandardFilter.first(%{"a" => "b"}) ["a", "b"]

floor(input)

@spec floor(term()) :: String.t()

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.StandardFilter.floor(1.2) "1" iex> Solid.StandardFilter.floor(2.0) "2" iex> Solid.StandardFilter.floor("3.5") "3"

join(input, glue \\ " ")

@spec join(term(), term()) :: term()

Join a list of strings returning one String glued by glue

iex> Solid.StandardFilter.join(["a", "b", "c"]) "a b c" iex> Solid.StandardFilter.join(["a", "b", "c"], "-") "a-b-c" iex> Solid.StandardFilter.join(["a", "b", "c"], 5) "a5b5c" iex> Solid.StandardFilter.join((0..3), "-") "0-1-2-3" iex> Solid.StandardFilter.join((3..0//-1), "-") "3-2-1-0" iex> Solid.StandardFilter.join(5, "-") "5"

last(input)

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

Returns the last item of an array.

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

lstrip(input)

@spec lstrip(term()) :: term()

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

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

map(input, property)

Map through a list of hashes accessing property

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

minus(input, number)

@spec minus(term(), term()) :: String.t()

Subtracts a number from another number.

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

modulo(dividend, divisor)

@spec modulo(term(), term()) :: String.t()

Subtracts a number from another number.

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

newline_to_br(iodata)

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

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

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

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

plus(input, number)

@spec plus(term(), term()) :: String.t()

Adds a number to another number.

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

prepend(input, string)

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

Adds the specified string to the beginning of another string.

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

remove(input, string)

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

Removes every occurrence of the specified substring from a string.

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

remove_first(input, string)

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

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

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

remove_last(input, string)

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

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

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

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

@spec replace(term(), term(), term()) :: String.t()

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

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

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

@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.StandardFilter.replace_first("Take my protein pills and put my helmet on", "my", "your") "Take your protein pills and put my helmet on"

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

@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.StandardFilter.replace_last("Take my protein pills and put my helmet on", "my", "your") "Take my protein pills and put your helmet on"

reverse(input)

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

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

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

round(input, precision \\ nil)

@spec round(term(), term()) :: number() | String.t()

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.StandardFilter.round(1.2) "1" iex> Solid.StandardFilter.round(2.7) "3" iex> Solid.StandardFilter.round(183.357, 2) "183.36" iex> Solid.StandardFilter.round(nil, 2) 0 iex> Solid.StandardFilter.round(5.666, 1.2) "5.7"

rstrip(input)

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

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

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

size(input)

@spec size(term()) :: non_neg_integer()

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

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

slice(input, offset, length \\ nil)

@spec slice(term(), term(), term()) :: String.t() | list()

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.StandardFilter.slice("Liquid", 0) "L"

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

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

iex> Solid.StandardFilter.slice([1, 2, 3], 1, 2) [2, 3]

sort(input)

@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.StandardFilter.sort(~w(zebra octopus giraffe SallySnake)) ~w(SallySnake giraffe octopus zebra)

sort_natural(input)

@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.StandardFilter.sort_natural(~w(zebra octopus giraffe SallySnake)) ~w(giraffe octopus SallySnake zebra)

split(input, pattern)

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

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

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

strip(input)

@spec strip(term()) :: 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.StandardFilter.strip(" So much room for activities! ") "So much room for activities!"

strip_html(iodata)

@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.StandardFilter.strip_html("Have <em>you</em> read <strong>Ulysses</strong>?") "Have you read Ulysses?"

strip_newlines(iodata)

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

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

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

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

sum(input, property \\ nil)

@spec sum(term(), term()) :: String.t()

Returns sum of all elements in an enumerable Allowing for an optional property to be passed in

iex> Solid.StandardFilter.sum([]) "0" iex> Solid.StandardFilter.sum([1, 2, 3]) "6" iex> Solid.StandardFilter.sum(1..3) "6" iex> Solid.StandardFilter.sum([%{"a" => 1}, %{"a" => 10}]) "0" iex> Solid.StandardFilter.sum([%{"a" => 1}, %{"a" => 10}], "a") "11"

times(input, operand)

@spec times(term(), term()) :: String.t()

Multiplies a number by another number.

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

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

@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.StandardFilter.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.StandardFilter.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.StandardFilter.truncate("Ground control to Major Tom.", 20, "") "Ground control to Ma"

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

@spec truncatewords(term(), term(), term()) :: 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.StandardFilter.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.StandardFilter.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.StandardFilter.truncatewords("Ground control to Major Tom.", 3, "") "Ground control to"

uniq(input)

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

Removes any duplicate elements in an array.

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

upcase(input)

@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.StandardFilter.upcase("aBc") "ABC"

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

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

url_decode(iodata)

URL decodes the string.

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

url_encode(iodata)

URL encodes the string.

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

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

where(input, key)

@spec where(term(), term()) :: list()

where(input, key, value)

@spec where(term(), term(), term()) :: 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.StandardFilter.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.StandardFilter.where(input, "available") [%{"id" => 1, "available" => true}, %{"id" => 3, "available" => true}]