Viewing: Liquid Template Syntax Basics
Filed Under: Templates
Liquid Template Syntax Basics
January 29th, 2008Introduction to the Cashboard Liquid template syntax, and a reference for Cashboard-specific Tags and Filters.
The Cashboard liquid VARIABLE REFERENCE can be found here.
Basics
There are two types of markup in liquid: Output and Tag.
Output is surrounded by {{ two curly brackets }}
Tags are surrounded by {% a curly bracket and a percent %}
Output blocks will always be replaced with the data which they reference.
For instance, if your liquid template has a company object exposed to it you can print the name of the company to the screen by referencing {{ company.name }}
Tags drive the logic of templates. They are responsible for loops and branching logic such as If / Else.
Output
Here is a simple example of Output:
Hello {{name}}
Hello {{person.name}}
Hello {{ 'seth' }}
Acceptable tags and Comments
We sanitize all templates, so that you may not use javascript or tags that might be harmful to the application. Disallowed tags include, but aren’t limited to:
- HEAD
- BODY
- SCRIPT
Comments
HTML comments are automatically sanitized by our system. If you wish to place comments in your code, do it with a liquid comment, like so:
{{ # This is a comment in liquid, and won't show up on the output }}
Filters
Output markup takes filters. Filters are simple methods. The first parameter is always the output of the left side of the filter. The return value of the filter will be the new left value when the next filter is run. When there are no more filters the template will receive the resulting string.
Hello {{ 'seth' | upcase }}
Hello seth has {{ 'seth' | length }} letters!
Hello {{ '*seth*' | format_text | upcase }}
Hello {{ 'now' | format_date }}
Cashboard Filter Reference
| capitalize | Capitalize all words |
|---|---|
| downcase | Lowercases any text within. |
| first |
Get the first element of the passed in array.
Ex: {{ invoice.line_items | first }}
|
| format_date |
Formats a date in your account’s selected date format.
(Set from the admin prefs.) |
| format_date_in_english |
Transforms a date into English of when that event happened.
(Ex 1 day ago, 2 months from now) |
| format_money | Transforms money string into your home currency, and possibly your client’s home currency if you’ve selected one for them. |
| format_text | Transforms text using Markdown syntax. Automatically inserts HTML tags like P, BR, etc. |
| image_path | Returns the proper image path for your file. Useful if you want to use it in a CSS document, or perhaps create your own image tags. |
| image_tag | Creates an IMG tag for a file you’ve uploaded to the Cashboard server for use inside your document. Ex: {{ 'image_name.jpg | image_tag }} |
| last |
Get the last element of the passed in array.
Ex: {{ invoice.line_items | last }}
|
| make_label | Makes an input label, or heading for a table. |
| number_to_percentage | Formats number as a percentage. |
| number_to_phone | Formats as a phone number. |
| number_with_delimiter | Shows a number with delimiter you specify.
Ex: {{ invoice.sales_tax | number_with_delimiter: ',' }}
|
| size | Return the size of an array or of an string |
| truncate |
Truncate a string down to x characters. Can be passed length and characters to add to the end which show truncation has happened.
Ex: {{ 'my long string' | truncate: 50, '...' }}
|
| truncatewords | Truncates a string down to a set number of words, input similar to above, except length is length in words, not characters. |
| sort | Sorts items in an array |
| upcase | Uppercases any text within. |
Tags
Tags are for the logic in your template. Below are the tags available to you inside Cashboard.
Variable Assignment
You can store data in your own variables, to be used in output or other tags as desired. The simplest way to create a variable is with the assign tag, which has a pretty straightforward syntax:
{% assign name = 'freestyle' %}
{% for t in collections.tags %}{% if t == name %}
<p>Freestyle!</p>
{% endif %}{% endfor %}
Another way of doing this would be to assign true/false values to the variable:
{% assign freestyle = false %}
{% for t in collections.tags %}{% if t == 'freestyle' %}
{% assign freestyle = true %}
{% endif %}{% endfor %}
{% if freestyle %}
<p>Freestyle!</p>
{% endif %}
If you want to combine a number of strings into a single string and save it to a variable, you can do that with the capture tag. This tag is a block which “captures” whatever is rendered inside it and assigns it to the given variable instead of rendering it to the screen. Here’s how it works:
{% capture attribute_name %}{{ item.title }}-{{ i }}-color{% endcapture %}
<label for="{{ attribute_name }}">Color:</label>
<select name="attributes[{{ attribute_name }}]" id="{{ attribute_name }}">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
If / Else
If else should be well known from any language imaginable. Liquid allows you to write simple expressions in the if.
{% if person %}
Hi {{ person.name }}
{% endif %}
{% if person.name == 'seth' %}
hi seth
{% endif %}
{% if person.name != 'seth' %}
you aren't seth
{% endif %}
{% if company.address == null %}
no address on file
{% endif %}
{% if invoice.line_items == empty %}
no items have been added to this invoice
{% endif %}
{% if invoice.total_due > 0 %}
you still need to pay this invoice
{% else %}
thanks for paying this invoice
{% endif %}
Case Statement
If you need more than one condition you can use the Case Statement
{% case line_item.quantity %}
{% when 0 %}
none
{% when 1 %}
one
{% when 2 %}
two
{% else %}
a few more...
{% endcase %}
Cycle
Often you have to alternate between different colors for similar tasks. Liquid has build in support for such operations using the cycle tag.
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
...Will result in…
one
two
three
one
If no name is supplied for the cycle group then its assumed that multiple calls with the same parameters are one group.
If you want to have total control over cycle groups you can optionally specify the name of the group. This can even be a variable.
{% cycle 'group 1': 'one', 'two', 'three' %}
{% cycle 'group 1': 'one', 'two', 'three' %}
{% cycle 'group 2': 'one', 'two', 'three' %}
{% cycle 'group 2': 'one', 'two', 'three' %}
...Will result in…
one
two
one
two
For Loops
Liquid allows for loops over collections. This is great in Cashboard to loop over things like the line items in an invoice.
{% for item in invoice.line_items %}
{{ item.description }}
{% endfor %}
During every for loop there are following helper variables available for extra styling needs:
forloop.length # => length of the entire for loop
forloop.index # => index of the current iteration
forloop.index0 # => index of the current iteration (zero based)
forloop.rindex # => how many items are still left?
forloop.rindex0 # => how many items are still left? (zero based)
forloop.first # => is this the first iteration?
forloop.last # => is this the last iteration?
There are several attributes you can use to influence which items you receive in your loop. limit lets you restrict how many items you get offset lets you start the collection with the nth item.
# array = [1,2,3,4,5,6]
{% for item in array limit:2 offset:2 %}
{{ item }}
{% endfor %}
# results in 3,4
Instead of looping over an existing collection, you can define a range of numbers to loop through. The range can be defined by both literal and variable numbers:
# if item.quantity is 4...
{% for i in (1..item.quantity) %}
{{ i }}
{% endfor %}
# results in 1,2,3,4
Includes
There are a few default liquid templates defined in Cashboard, but the system allows you to create as many as you want.
This is so you may take advantage of liquid includes. You can include any of these templates inside another template, like so.
{% include 'template_name' %}
This comes in handy if you wish to share layout, css information, or html between templates. One default template in particular is company information.
By default, this stores the company address output table for invoices and estimates. We re-use the html here, by passing it what company we wish to display information for:
{% include 'company' for account.company %}
Inside the company template, you can then access variables from account.company as the template name (company)
{{ company.name }}
{{ company.address }}
Tables
Liquid can also create table rows and cells for you. (you still need to wrap a table tag around the tablerow instruction)
<table>
{% tablerow item in items cols: 3 limit: 12 %}
{{ item.variable }}
{% endtablerow %}
</table>
You can also find out whether a table cell is the first or last column in a row or directly query the column number:
tablerowloop.length # => length of the entire for loop
tablerowloop.index # => index of the current iteration
tablerowloop.index0 # => index of the current iteration (zero based)
tablerowloop.rindex # => how many items are still left?
tablerowloop.rindex0 # => how many items are still left? (zero based)
tablerowloop.first # => is this the first iteration?
tablerowloop.last # => is this the last iteration?
tablerowloop.col # => index of column in the current row
tablerowloop.col0 # => index of column in the current row (zero based)
tablerowloop.col_first # => is this the first column in the row?
tablerowloop.col_last # => is this the last column in the row?
{% tablerow item in items cols: 3 %}
{% if col_first %}
First column: {{ item.variable }}
{% else %}
Different column: {{ item.variable }}
{% endif %}
{% endtablerow %}


Sorry, comments are closed for this article.