Liquid dataTypes

Updated on 20-October-2016 at 10:16 AM

Business Catalyst End of life announcement - find out more details.

With the introduction of proper data types it is now possible to convert variables from one type to another and perform various operations. Take a look at the convert filter to see the possible combinations you can use.

number

You can use numeric variables together with the mathematical filters. To convert a string variable to numeric use:

Note: If you need to use decimals use a single dot as the separator.

{{ "2015" | convert: "number" }} outputs 2015 in numeric format

{{ "2015" | convert: "number" | plus: 30 }} outputs 2045 in numeric format

Assuming your current URL is mydomain.com?somevariable=4 {{globals.get.somevariable}} will output "4" as string. You can convert this to numeric and use it for mathematical operations:

{{ globals.get.somevariable | convert: "number" | plus: 30 }} will output 34

To assign a numeric value to a variable you should simply use:

{% assign someNumber = 10 %}

dateTime

A cool feature is now you can create your own dateTime variables and use them together with system dates. You can use the creationDate, releaseDate and expiryDate variables and compare them against your own dates, compare them against the current date or add/substract time.

Note: All dateTime variables are using the ISO 8601 standard.

To create your own dateTime variables you can declare them as string and convert them using the convert filter. After the dateTime variable is created you can use it to perform date related.

{{ "2015-01-07" | convert: "date" }}

outputs 2015-01-07T00:00:00 in dateTime format

For dateTime related samples see the Code snippets section. Also take a look at the Date filters section for an index of the filters you can use to format dateTime variables.

timeSpan

timeSpan type variables are only created when substracting one dateTime variable from another dateTime variable.

For example, below we will declare two dateTime variables, substract date1 from date2 and see what the timeSpan result looks like

{% assign myVar = "2015-01-07T15:52:30" | convert: "date" %}
{% assign diffDays = globals.site.dateNow | minus: myVar %}
{{diffDays}}

In this example the resulting diffDays timeSpan variable will be 5.21:11:40.6027920. You can also use the special timeSpan switches to further process this output.

boolean

This data type is pretty straight forward. It is used for true/false comparisons. You can create your own boolean type variables by converting "true" or "false" like so:

{{ "true" | convert: "boolean" }} - this will create a true boolean-type variable.

To assign a boolean value to a variable you should simply use:

{% assign myVariable = false %}

currency

This data type is used to add the site's currency to an ammount.

A site may have different currencies, one for each domain added into the Site Domains panel. For example your site may have two domains, one for US and one for UK. In this case on the US domain {{salePrice | currency}} will output $100.00 and on the UK domain will output ?100.00.

Similarly any number assigned the convert filter will add the site's Country currency: {{2 | currency}} will output $2.00 on the US domain.

How to compare variables

When comparing variables always make sure you are using the correct data types. If the operand types are mismatched the system tries to make the operation work by converting one of the operands like this:

  • comparing string with numeric - the system tries to convert the string operand to numeric. If this is not possible both operands will be treated as string. For example:
    • 5 > "7" - "7" will be converted to numeric and the operation will be false
    • 5 > "a132" - "a123" cannot be compared to numeric, this will be treated as "5" > "a123"
  • comparing string with dateTime -the system will try to convert the string operand to a dateTime variable, if this fails the operands will be compared as string. For example:
    • 2015-01-13T10:47:54 > "2012-01-13T10:47:54" - the second operand will be converted to dateTime and the operation will be true
    • 2015-01-13T10:47:54 > "Jul 2012" - the second operand cannot be compared to dateTime and the comparison will be treated as a string to string comparison.

To convert variables take a look at the convert filter in the Supported filters article.