View Source Liquex.Filter behaviour (liquex v0.12.0)

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

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.

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.

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.

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.

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.

Removes every occurrence of the specified substring from a string.

Removes every 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.

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.

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

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.

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

@type filter_t() :: {:filter, [...]}

Callbacks

Link to this callback

apply(any, filter_t, map)

View Source
@callback apply(any(), filter_t(), map()) :: any()

Functions

@spec abs(String.t() | number() | nil, any()) :: number()

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
@spec append(String.t(), String.t(), map()) :: String.t()

Appends text to the end of value

Examples

iex> Liquex.Filter.append("myfile", ".html", %{})
"myfile.html"
Link to this function

apply(mod \\ __MODULE__, value, arg, context)

View Source
@spec at_least(number() | binary(), number() | binary(), map()) :: number()

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
@spec at_most(number(), number(), map()) :: number()

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
@spec capitalize(String.t(), map()) :: String.t()

Capitalizes a string

Examples

iex> Liquex.Filter.capitalize("title", %{})
"Title"

iex> Liquex.Filter.capitalize("my great title", %{})
"My great title"
@spec ceil(number() | String.t() | nil, map()) :: number()

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
@spec compact([any()], map()) :: [any()]

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]

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]
Link to this function

date(value, format, context)

View Source

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"
Link to this function

default(value, def_value, _)

View Source

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"
Link to this function

divided_by(value, divisor, _)

View Source

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, 7.0, %{})
2.857142857142857

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 &#39;James &amp; the Giant Peach&#39;?"

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 &lt; 2 &amp; 3", %{})
"1 &lt; 2 &amp; 3"
@spec filter_name(filter_t()) :: String.t()

Returns the first item of an array.

Examples

iex> Liquex.Filter.first([1, 2, 3], %{})
1

iex> Liquex.Filter.first([], %{})
nil
@spec floor(binary() | number() | nil, any()) :: integer()

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

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(list(), 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()], 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.357, 12, %{})
171.357
@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.357, 12, %{})
3.357
@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.357, 12, %{})
195.357
Link to this function

prepend(value, prepender, _)

View Source

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"
Link to this function

remove(value, original, context)

View Source

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 "
Link to this function

remove_first(value, original, context)

View Source

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"
Link to this function

replace(value, original, replacement, _)

View Source

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"
Link to this function

replace_first(value, original, replacement, _)

View Source

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"

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"]
Link to this function

round(value, precision \\ 0, context)

View Source
@spec round(binary() | number() | nil, binary() | number() | nil, any()) :: number()

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

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
Link to this function

slice(value, start, length \\ 1, _)

View Source

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"

Sorts items in an array in case-sensitive order.

Examples

iex> Liquex.Filter.sort(["zebra", "octopus", "giraffe", "Sally Snake"], %{})
["Sally Snake", "giraffe", "octopus", "zebra"]
Link to this function

sort(list, field_name, _)

View Source

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"]
Link to this function

sort_natural(list, field_name, _)

View Source
Link to this function

split(value, separator, _)

View Source

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.

Examples

iex> Liquex.Filter.strip_html("Have <em>you</em> read <strong>Ulysses</strong>?", %{})
"Have you read Ulysses?"
Link to this function

strip_newlines(value, _)

View Source

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

Examples

iex> Liquex.Filter.strip_newlines("Hello\nthere", %{})
"Hellothere"
Link to this function

times(value, divisor, _)

View Source

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(183.357, 12, %{})
2200.284
Link to this function

truncate(value, length, ellipsis \\ "...", _)

View Source

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"
Link to this function

truncatewords(value, length, ellipsis \\ "...", _)

View Source

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"

Removes any duplicate elements in an array.

Examples

iex> Liquex.Filter.uniq(~w(ants bugs bees bugs ants), %{})
["ants", "bugs", "bees"]

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}]
Link to this function

where(list, key, value, _)

View Source

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}]