In Consonance, document templates use Liquid, a templating language which works a bit like a mail merge. Liquid allows a combination of static text, and placeholders for text that will be dynamically inserted. When a Liquid template is used, the placeholders are replaced with data.
We call these placeholders ‘markup’. There are two kinds of markup – outputs, and tags.
Outputs dynamically output a value of some type. They are defined using {{ ... }}
Tags contain logic that calculate or modify data, and they are defined using {% ... %}
The three dots in both of those examples represent Liquid syntax, or data. It’s the starting and ending characters we are interested in, at this point.
Here is an example of some Liquid markup in action. (This is simplified whilst we are talking about concepts, so don’t use this in a Consonance document template! You will learn how to write valid examples soon.)
1
2
Title: {{ title }},
{% if on_sale %}Now on sale{% endif %}
When you combine a document template
in Consonance with some data, the end result can be thought of as a document
. If title is The Hobbit and on_sale is true
, the document produced would be:
1
2
Title: The Hobbit
Now on sale
If on_sale is false, the second line is blank.
Output markup
We saw there that, to insert a value, you wrap its name in double curly braces. We can also insert literal text if we put it in quotes:
1
2
3
{{ title }}
{{ "Plain text in quotes" }}
{{ 42 }}
If title is The Hobbit, this produces:
1
2
3
The Hobbit
Plain text in quotes
42
Text can be wrapped in either single or double quotes, whereas numbers have no quotes around them.
Filters
You can alter the value of data in an output by using a filter. Put a pipe | after the value, then the filter name:
1
{{ "the hobbit" | upcase }}
That produces this document, where the lowercase string the hobbit
has been altered to be uppercase:
1
THE HOBBIT
Some filters need extra information, called arguments, written after a colon. (You might also see arguments referred to as parameters.) Separate several arguments with commas:
1
{{ "The Hobbit" | replace: "The", "A" }}
That produces a document where The
is replaced with A
:
1
A Hobbit
Filters can be chained, with each filter working on the result of the one before:
1
{{ "hobbit" | capitalize | append: "!" }}
The resultant document has two alterations: firstly, the lowercase string is capitalised, and secondly, an exclamation mark is appended to the string:
1
Hobbit!
Tags
Tags contain commands, used to control the text that is produced.
Some can be used to conditionally produce text. Many tags come in pairs: an opening tag, the content, and a matching closing tag whose name starts with end. In this example, look for the opening and closing tags, on different lines:
1
2
3
{% if name == "Sam" %}
Welcome back, Sam.
{% endif %}
Other tags sit on one line, and exist without a closing partner:
1
{% assign title = "The Hobbit" %}
Objects and properties
Objects and properties are the way that Consonance data is included in templates.
Objects, such as products
, to-dos
, production runs
, or contacts
, each have a set of properties. For example an individual product
object has properties such as publication_date
(not publication date
or Publication date
), title
, and work
.
Properties have a data type, such as date
, string
, or boolean
(true or false), and are often themselves an object. When the property is an object, the object itself has properties (though see “Troubleshooting below when objects are nil).
A property of an object might be a single value (e.g. work.title
) or it might be a list of multiple values (e.g. product.prices
). A list of values is called an array”.
A property might in many cases have no value at all, for example when a product does not have an assigned ISBN, or a work is not assigned to an imprint.
Dot notation
In many cases a property of an object is itself an object. For example, work
is a property of product
, and work has properties of its own.
Putting a dot (or full stop, or period) after the name of the object, and then the name of the property, is a way to chain data together, so that you get the object’s property’s value:
1
2
{{ product.work.title }}
{{ price.product.ebook? }}
Square brackets
Alternatively, when the property name contains spaces or other unusual characters, you might have to use square brackets, like this:
1
2
{{ person["first name"] }}
{{ person[field_name] }}
Arrays
An array is an ordered collection of items, like a list. Being ordered
means you can rely on the data always appearing in the list at the same position. Use a number in square brackets to pick the data. Counting starts at zero, so the first item is [0].
Example: if the array is ["red", "blue", "green"] and the following Liquid is run:
1
{{ colours[1] }}, {{ colours[0] }}
…the resultant document would be blue, red
.
Three shortcut options work on arrays: first, last, and size.
1
2
3
{{ colours.first }}
{{ colours.last }}
{{ colours.size }}
.size also works on strings, where it returns the number of characters in the string.
Troubleshooting
- A property that doesn’t exist raises an error. If you misspell a property name, or ask an object for a property it doesn’t have, you will see an error message instead of a blank. This applies anywhere in a chain.
- A property that exists, but has no value, prints as blank, as long as it is the last part of the chain.
- A property that has no value cannot be accessed. If
person.addressis empty, then{{ person.address.city }}raises an error.
To avoid that error, check first with if:
1
2
3
{% if person.address %}
{{ person.address.city }}
{% endif %}
It is very common to need to find only objects that match a particular condition. For example you might want to find the audiobook products for a work. Sometimes the logic for doing this is not very straightforward, and might normally rely on knowledge of ONIX codes (for example that an audiobook has a product form beginning with the letter A
). In many cases Consonance properties have been provided to make these common conditional checks more simple, and they are documented in the data reference within the app.
To go through every item in an array, use a for loop. Read more on the Tags page.
Whitespace control
Tags that print nothing still add in whitespace, which can cause unwanted blank lines. To remove, add a hyphen inside the bracket:
{%- or {{- removes whitespace before the tag.
-%} or -}} removes whitespace after the tag.
Use either or both. The hyphen removes all spaces and line breaks up to the next visible character.
Without hyphens:
1
2
3
4
5
<ul>
{% for n in names %}
<li>{{ n }}</li>
{% endfor %}
</ul>
This leaves extra blank lines between the list items:
1
2
3
4
5
6
<ul>
<li>Alice</li>
<li>Bob</li>
</ul>
With hyphens:
1
2
3
4
5
<ul>
{%- for n in names %}
<li>{{ n }}</li>
{%- endfor %}
</ul>
This results in a better formatted document:
1
2
3
4
<ul>
<li>Alice</li>
<li>Bob</li>
</ul>
Comments
Notes to yourself, or code you want to switch off temporarily, can be wrapped in a comment. Nothing inside a comment is added to the resultant document, and any Liquid inside it is not run.
Block comment: use for comments of any length.
1
2
3
{% comment %}
Remove this paragraph once the new price is confirmed.
{% endcomment %}
Inline comment: for a short comment on a single tag. Start with #.
1
{% # This heading only shows for subscribers %}
A multi-line inline comment needs a # at the start of every line:
1
2
3
4
{%
# This is a longer note
# that runs over two lines
%}
Consonance-flavoured Liquid
The Liquid templating language is written and maintained by Shopify and released open source so other companies can use it and extend it. Consonance uses the basic Shopify Liquid library, and we have also written additional objects, properties, and filters.
There are various sources of documentation on the internet, found by searching up Liquid templating
, and most are at least generally useful. The documentation in the Consonance app and website is specific to Consonance – so filters and tags that you find within will only be relevant in Consonance, and some filters, tags and objects that you read about on other websites may not work in Consonance. For instance, Consonance does not have any use for a checkout
feature, so Shopify’s Liquid object checkout is not valid within Consonance. Conversely, Consonance includes some filters we have written ourselves, so they won’t work on other websites. Always raise a support ticket to ask for help and we can provide you with valid Liquid markup to get you started, or unblock you if you are stuck.
The Consonance Document Template Library contains some useful starter packs and is added to regularly: find that list on the Document templates list page in the app.