Skip to content

Liquid Filters

Peter Johnson edited this page Feb 11, 2023 · 38 revisions

Liquid Filters

⚠️⚠️ THIS WIKI IS BEING USED TO JOT DOWN IDEAS AT PRESENT. DON'T ASSUME ANYTHING IS FIXED. ⚠️⚠️

⚒️ MORE TO COME: Check back later

The filters listed on this page are based on, and have similar syntax and purpose to, Liquid filters. They are not guaranteed to work in exactly the same way as the Liquid versions, but they emulate the behaviour as closely as is practical.

🗒️ Not all liquid filters will necessarily be implemented.

🗒️ Those filters listed here that are not yet implemented are marked with a ⛔ symbol.

The examples included with many of the filters are copied or adapted from the Liquid documentation and so use the Liquid | pipe and : parameter symbols instead of the > and < symbols normally used by rpt.

A link to the original Liquid documentation is included with each filter.

⛔ Returns the absolute value of a numberish value provided on its input. If the input is not numberish then an error is reported.

Parameters: none

Input type: numberish

Output type: Number (Int if input was, or was converted to, Int or Float if input was, or was converted to, Float)

Example:

{{ -17 | abs }}
{{ -56.42 }}
{{ 4 | abs }}
{{ "42" | abs }}

Outputs

17
56.42
4
42

⛔ Appends the text given as a parameter to the input and returns the result.

Parameter: text to be appended, type textish.

Input type: textish

Output type: text

Example:

{{ "/my/fancy/url" | append: ".html" }}

Outputs

/my/fancy/url.html

⛔ Rounds an input Float up to the nearest whole number.

Parameter: none.

Input type: Floatish

Output type: Int

Example:

{{ 1.2 | ceil }}
{{ 2.0 | ceil }}
{{ 183.357 | ceil }}

Outputs:

2
2
184

⛔ Formats a DateTime as a Text value according to the format string passed as a parameter. The format string has the same syntax as strftime.

Parameter: date formatting string, type Text.

Input type: DateTime

Output type: Text

Example (assuming @now is returning 2015-07-17):

{{ @now | date: "%a, %b %d, %y" }}
{{ @now | date: "%Y" }}

Outputs:

Fri, Jul 17, 15
2015

⛔ Divides the number on the input by the number passed as a parameter.

The result is of the same type as the divisor (i.e. the parameter). So if the divisor is an integer then the result will be rounded down to the nearest integer. In the case where the divisor is floating point then the result will also be floating point, with no rounding.

Parameter: the divisor (must not be 0), type Int or Float

Input type: Int or Float

Output type: Int or Float (same as parameter)

Example:

{{ 20 | divided_by: 7 }}
{{ 20 | divided_by: 7.0 }}
{{ 25.5 | divided_by: 8 }}
{{ 25.5 | divided_by: 8.7 }}

Outputs

2
2.8571428571
3
2.9310344828

Notes:

  1. To force floating point division when the parameter is an integer use an embedded placeholder with the rpt int-to-float filter, as follows:

     {{ 20 | divided_by: {{ 7 | int-to-float }} }}
    
  2. Dividing a floating point value by an integer is equivalent to doing a floating point division and taking the floor of the result. So the following two placeholders will return the same result:

     {{ 42.5 | divided_by: 7 }}
     {{ 42.5 | divided_by: 7.0 | floor }}
    
  3. See also the less complicated rpt div (integer division) and divide (floating point division) filters.

⛔ Converts each character of text from the input into lower case.

Any type other than text is first converted to text and then processed.

Parameters: none

Input type: Textish

Output type: Text

Example:

{{ "Parker Moore" | downcase }}
{{ "apple" | downcase }}

Outputs

parker moore
apple

⛔ Rounds an input Float down to the nearest whole number.

Parameter: none.

Input type: Floatish

Output type: Int

Example:

{{ 1.2 | floor }}
{{ 2.0 | floor }}
{{ 183.357 | floor }}

Outputs:

1
2
183

⛔ Subtracts the number given as a parameter from the input number and outputs the result.

Parameter: number to be subtracted from input, type Numeric.

Input type: Numeric

Output type: Int if both the input and parameter are Int, otherwise Float.

Example:

{{ 4 | minus: 2 }}
{{ 16.9 | minus: 4.2 }}
{{ 183.357 | minus: 12 }}

Outputs

2
12.7
171.357

⛔ Returns the remainder of a division operation. The number input to the filter is divided by the number passed as a parameter.

If the input is an integer then the output is also an integer, otherwise the output is floating point.

Parameter: the divisor (must no be zero), type Int.

Input type: Int or Float

Output type: Int or Float (same type as input)

Example:

{{ 3 | modulo: 2 }}
{{ 24 | modulo: 7 }}
{{ 183.357 | modulo: 12 }}

Outputs

1
3
3.357

⚠️ The Liquid documentation for this filter does not state whether the divisor must be an integer. However all the given examples use an integer divisor, implying this may be required. This is the approach taken by the rpt implementation.

⛔ Adds the number given as a parameter to the input number and outputs the result.

Parameter: number to be added to input, type Numeric.

Input type: Numeric

Output type: Int if both the input and parameter are Int, otherwise Float.

Example:

{{ 4 | plus: 2 }}
{{ 16.0 | plus: 4.2 }}
{{ 183.357 | plus: 12 }}

Outputs

6
20.2
195.357

⛔ Prepends the text given as a parameter to the input and returns the result.

Parameter: text to be prepended, type textish.

Input type: textish

Output type: Text

Example:

{{ "apples, oranges, and bananas" | prepend: "Some fruit: " }}

Outputs

Some fruit: apples, oranges, and bananas

⛔ Rounds an input Float off to the nearest whole number or, if an argument is provided, to the specified number of decimal places.

Parameter: number of decimal places to round to (optional: default is 0), Type Int.

Input type: Floatish

Output type: Int

Example:

{{ 1.2 | round }}
{{ 2.7 | round }}
{{ 183.357 | round: 2 }}

Output:

1
3
183.36

⛔ Multiplies the number given as a parameter by the input number and outputs the result.

Parameter: number to be multiplied by input, type Numeric.

Input type: Numeric

Output type: Int if both the input and parameter are Int, otherwise Float.

Example:

{{ 3 | times: 2 }}
{{ 24.7 | times: 7.3 }}
{{ 183.357 | times: 12 }}

Outputs

6
180.31
2200.284

⛔ Converts each character of text from the input into upper case.

Any type other than text is first converted to text and then processed.

Parameters: none

Input type: Textish

Output type: Text

Example:

{{ "Parker Moore" | upcase }}
{{ "APPLE" | upcase }}

Outputs

PARKER MOORE
APPLE
Clone this wiki locally