Working with Liquid
WebinOne has implemented the Liquid templating language to allow easy and powerful control of layout components mixed with server-side logic for your site data and content.
Liquid uses a combination of objects, tags, and filters inside template files to display dynamic content.
For a deeper dive into Liquid, or if you are new to Liquid, see our free online course to get you up to speed: Learning Liquid for WebinOne.
Liquid Syntax
Objects and Variables
Objects contain the content that Liquid displays on a page. Objects and variables are displayed when enclosed in double curly braces: {{
and }}
.
The default syntax used throughout the system when inserting liquid property tags will use square brackets, single quotes and 'capital camel case' for the property name. See the following example:
{{this['PropertyName']}}
Although, keep in mind that all property names, including custom properties added in Custom Modules, are aliased to a single word all in lowercase (flat case) for the purpose of referencing that data via Liquid.
The 'capital camel case' used above is for readability only as liquid property tags are not case sensitive.
You can choose to use an upper or lowercase syntax here.
Furthermore, as all property names are aliased to a single 'flat case' word, you can optionally use a shorthand method for referencing your property names. Such as the example following:
{{this.PropertyName}} OR {{this.propertyname}}
Notice the removal of the square brackets and single quotes and the addition of the dot (.) separator. The dot separator is required wherever the square bracket syntax is NOT used.
The square brackets and single quote syntax is part of the liquid syntax for scenarios where property names have spaces included. This is not required in WebinOne.
Tags
Tags create the logic and control flow for your layouts. The curly brace percentage delimiters ({%
... %}
) and the text that they surround do not produce any visible output when the template is rendered. This lets you assign variables and create conditions or loops without showing any of the Liquid logic on the page.
{% assign name = "Alex" %}
{% if name %}
My name is {{ name }}.
{% endif %}
My name is Alex.
Tags can be categorized into various types.
You can read more about each type of tag in the following documentation articles:
Liquid Filters
Filters manipulate the output of a Liquid object or variable.
They can be used when rendering object data and variables via double curly braces {{
... }}
.
{% assign name = "Alex" %}
My name is {{ name | upcase }}.
My name is ALEX.
They can also be used when assigning variables.
{% assign name = "Alex" | upcase %}
My name is {{ name }}.
My name is ALEX.
Liquid Data Types
Data being accessed by Liquid can be in various formats, which can be interpreted and handled in different ways.
These data types are as follows:
string
number
boolean
true
or false
declarations. They are defined withour quotation marks, however, if quotation marks are used Liquid will generally interpret the value as a true or false boolean. Other data types can also resolve to true or false values, which is described in more detail here.array
null
For more detailed explaination and usage of these data types in Liquid, see the Liquid Learning guide here.
null
and Empty Properties
Often scenarios arise where properties will have no value (empty), or are not present in the Liquid output and you may want to check for this condition in your Liquid code.
In WebinOne's .NET implementation of Liquid, null
is used as a special value when a value is empty or a property is not present - both conditions will resolve to a null
result.
Therefore, a reliable way to check for all empty or not present conditions can be achieved as per the following example, where we are rendering something if the property is ==
(equal to) null
:
{% if name == null %}
There is no name present (as the `name` variable is not defined yet).
{% endif %}
{% assign name = "" %}
{% if name == null %}
There is no name present (the `name` variable is now defined but has an empty value).
{% endif %}
However, using null
in our condition we cannot know specifically if the property is just empty or actually does not exist. For this, we can specifically check if just the property exists:
{% unless name %}
There is no name property defined.
{% endunless %}
{% assign name = "" %}
{% if name %}
Now there is a name property (it may be empty or have a value).
{% endif %}
Learning Liquid - Free Online Course
For a deeper dive into Liquid, or if you are new to Liquid, see our free online course to get you up to speed: Learning Liquid for WebinOne.