Liquid Filters

For the filters native to the Liquid language, please see the Liquid Filters Documentation.

Socotra also supports an extended set of Liquid filters, as described here. Some of the filters are only relevant for document production, others are relevant for calculations, and some apply to both.

Liquid Filters for Document Templates

format_number_currency

Formats a number as a currency appropriate to the specified region, including decimal and thousands separators. Inputs:

  1. ISO 639 alpha-2 or alpha-3 language code

  2. ISO 3166 alpha-2 country code

This evaluates to “€123.456,70”:

{{ 123456.7 | format_number_currency: "de","DE" }}

format_number

Formats a number based on the specified region, including decimal and thousands separators. Inputs:

  1. ISO 639 alpha-2 or alpha-3 language code

  2. ISO 3166 alpha-2 country code

This evaluates to “123,456.7”:

{{ 123456.7 | format_number: "en", "US" }}

format_number_pattern

Formats a number according to the specified pattern.

{{ vehicle_value | format_number_pattern: "0.0" }}

timestamp_millis_add

Add to a timestamp (timezone-aware).

{% assign new_timestamp = timestamp | timestamp_millis_add: "day", -1 %}

timestamp_millis_round_day_floor

Round a timestamp to the beginning of the day.

{% assign begin_of_today = "now" | get_timestamp_millis | timestamp_millis_round_day_floor %}

timestamp_millis_round_day_ceiling

Round a timestamp to the beginning of the next day.

{% assign begin_of_tomorrow = "now" | get_timestamp_millis | timestamp_millis_round_day_ceiling %}

lookup

Look up a value from a table in the /tables folder within the product’s configuration.

{% assign rate = "my_rate_table" | lookup: my_lookup_key %}

The table must be a CSV file with only two columns, named key and value, lowercase. For example:

key,value
Car,1
SUV,1.1
Motorcycle,2
Pickup,1.1

In the filter call, the name of the table and the key to look up both must be strings. Looking up 12 will not work, but "12" will:

{% assign rate = "my_rate_table" | lookup: "12" %}

If a lookup fails (due to bad table name, bad key, or problem with your table) the return value will be undefined. In this case the will_not_be_defined variable will not be defined at all:

{% assign will_not_be_defined = "example_table" | lookup: "bad key" %}

timestamp_millis_print

Format and display a timestamp using a given format date/time string.

{{ coverageStartTimestamp | timestamp_millis_print: "d MMM YYYY" }}

Liquid Filters for Premium Calculations

Note

For Liquid’s native math filters, please see the Liquid Math Filters documentation.

set_month_premium

A call to this filter (or set_year_premium, see below) is required. This sets the input as the premium for one month of coverage.

Socotra will automatically scale this amount to match the duration of coverage, with straight-line proration for partial months. So, setting $100 as the monthly premium amount will result in total premium of $550 if the term being rated is five and a half months.

{{ 100 | set_month_premium }}

set_year_premium

This is the same as set_month_premium except that the value will be divided by 12 before being processed. For example:

{{ 100 | set_month_premium }}

produces the same results as:

{{ 1200 | set_year_premium }}

set_month_technical_premium

The optional filter set_month_technical_premium will record the technical premium on the peril for future analysis. These may only be used in a peril premium calculation, i.e. [peril_name].calculation.liquid.

Socotra will automatically scale this amount to match the duration of coverage, with straight-line proration for partial months/years

{{ 85 | set_month_technical_premium }}

set_year_technical_premium

This filter works the same as set_month_technical_premium except the value passed will be divided by 12 before being processed.

{{ 1020 | set_year_technical_premium }}

add_month_commission

This filter adds a commission to the peril. It requires both a numeric amount and a recipient code, which can be any string. Socotra treats the numeric amount as being in the default currency for the instance.

Like premium, Socotra will automatically scale this amount to match the duration of coverage, with straight-line proration for partial months.

{{ 10 | add_month_commission: recipient_id }}

add_year_commission

This filter works the same as add_month_commission except the value passed will be divided by 12 before being processed.

{{ 120 | add_year_commission: recipient_id }}

pow

The power function

{% assign x_squared = x | pow: 2 %}

Multi-Use Filters

These filters are relevant to both document rendering and premium calculation:

Liquid Tips

Here are a few more suggestions for working with Liquid files.

Floating Point Assignments

When expressing decimal / float numbers, use a leading 0: 0.7 rather than .7. This will work as expected:

{% assign myVariable = 0.7 %}

This will not define your variable:

{% assign myVariable = .7 %}

Lookup Table Syntax

Values in lookup tables may be enclosed in quotes, or not. In the example below, the "SUV" key will be read as SUV:

key,value
Car,1
"SUV",1.1
Motorcycle,2
Pickup,1.1

Note

Though the placement of tables in a config might suggest that tables are scoped at the product level, they are actually scoped at the tenant level. We recommend that each table have a unique name to avoid unexpected lookup results. One common technique is to preface each table with the product name.

Debugging Liquid Template Rendering Errors

  • To debug calculations of premiums, taxes, or fees, create a variable ({% assign myVar = foo %}) for any value you want to see and then you can print out all assigned variables using this API endpoint or this Python Script .

  • To debug document creation, you can render variables or values document using {{ value }}.

  • If you get an error like this: Error rendering liquid template revision... it might be because you used the wrong name to refer to one of your field values. For example, perhaps you used select_experience but the field name was actually years_of_experience.

  • Note that for calculations, specifying fractional values less than one requires a leading zero; for example, use 0.12 instead of .12.