Every value in Liquid has a type – for instance, a value might be a string, or a number. The type decides what you can do with the value. It also controls whether Liquid treats it as yes
or no
when you are using in some logic.
Liquid value types
| Type | Description | Example |
|---|---|---|
| String | Text | "The Hobbit" |
| Number | A whole number or a decimal | 100, 9.95 |
| Boolean | A yes/no value | true, false |
| Nil | Nothing: the absence of a value | nil |
| Array | An ordered list of values | [red, green, blue] |
| Object | A bundle of named properties | A product, with a publication date, and an ISBN-13 |
String
A string is a piece of text. Write it inside single or double quotes. Use whichever kind of quote is not in your text:
1
2
{{ "The Captain's Revenge" }}
{{ '"...dramatic, urgent, timely" The Observer' }}
Number
Numbers are written without quotes. A number with a decimal point is a decimal, whereas one without is a whole number (integer).
1
2
{{ 100 }}
{{ 9.95 }}
Whole numbers behave differently from decimals in division. Dividing whole numbers gives a whole number:
1
2
{{ 10 | divided_by: 4 }} # 2
{{ 10 | divided_by: 4.0 }} # 2.5
Boolean
true and false are the two boolean values. They print in a resultant document as the words true and false.
The string false
, in quotes, is not the same as the boolean false. Liquid treats it as yes
.
Nil
nil means no value
. If, for example, a product has not been allocated a publication date, it would be nil.
1
2
{{ product.subtitle }} # This prints as blank if the product has no subtitle
{% if product.subtitle %}Yes{% else %}No{% endif %} # This would print as "no"
Array
An array is an ordered series of values. Make an array in Liquid by splitting a string at a separator using the split filter:
1
2
{% assign subjects = "fantasy,crime,horror" | split: "," %}
{{ subjects[1] }} # fantasy
Printing a whole array joins its items together, with nothing in between:
1
{{ subjects }} # fantasycrimehorror
So to use a separator, use the join filter, with a string argument that says what character you want to use:
{{ subjects | join: ", " }}
gives
fantasy, crime, horror
And
{{ subjects | join: " ❄️ " }}
gives
fantasy ❄️ crime ❄️ horror
Object
An object bundles several named values, called properties, together, such as a product with a publication_date and a title.
1
{{ product.publication_date }}
Read the Consonance data reference for a full and up-to-date list of the objects and properties written by the Consonance team that give you access to the data in your account.
Truthiness
Whenever a value needs to be processed logically, for example in {% if name %}, the outcome depends on whether the value is interpreted as yes
(truthy) or no
(falsy).
Only two values are interpreted as no
: false and nil. Everything else is interpreted as yes
.
1
2
3
{% if 0 %}Zero is interpreted as yes.{% endif %}
{% if "" %}An empty string is interpreted as yes.{% endif %}
{% if work.subtitle %}A subtitle with no value is interpreted as no.{% endif %}
1
2
Zero is interpreted as yes.
An empty string is interpreted as yes.
blank
Use == (is equal to) or != (is not equal to) with blank:
1
2
{% if work.subtitle == blank %}No subtitle{% endif %}
{% if work.subtitle != blank %}{{ work.subtitle }}{% endif %}
Examples
Copy and paste any of the following code snippets into a Consonance work
document template.
Show something only if it has content:
1
2
3
{% if work.title != blank %}
<h2>{{ work.title }}</h2>
{% endif %}
Show something only if an array has items:
1
2
3
{% if work.thema_codes != blank %}
Themas: {{ work.thema_codes | join: ", " }}.
{% endif %}
Look inside something that might not be there. Some properties are optional objects: a work may or may not have an imprint, for example. Asking an empty object for its value raises an error, so check for it first:
1
2
3
4
5
{% if work.imprint %}
{{ work.imprint.name }}
{% else %}
No imprint given.
{% endif %}
The if is true only when imprint has a value, so the name lookup inside it is safe.
Provide a fallback when something is blank: use the default filter:
1
{{ work.title | default: "Untitled work" }}