Liquid Syntax is an open-source template language created by Shopify. Jekyll uses it to load data dynamically, access data across the site, implement conditional logics like if, for, case etc.,
For a beginner, this might look like an alien programming which doesn’t resemble any of the programming languages that he/she may have seen before.
But I assure you that it is probably the simplest and comprehensive language that I have seen so far. Take this snippet for example,
So simple isn’t it. I wish other programs were also this easy and did not make a fuss whenever I miss a semicolon.
Let’s begin learning Liquid keeping Jekyll in mind. There are 3 things that we should know before we jump right in.
Tags | Objects | Filters |
---|---|---|
{% %} | {{ }} | {{ | }} |
Conditions | Output | Modify Output |
Tags are functional. They usually tell what to do and how to do something. They are used to implement control flows, iterations etc., The best example for this would be the one I have mentioned above - legal age for drinking beer.
Along with conditional statements, tags are also used for special purposes like including a file from _includes
folders. I have also discussed it here.
These statements will look for the files head.html
, header.html
and footer.html
and insert them in the places where the tags are defined. I have discussed this here as well.
Liquid tags and objects may not work on a page if you have not defined front matter in it. At least blank front matter like this
---
---
Objects are used to output dynamic data on a page. In Jekyll, we have front matter title
for almost all pages and posts. This can be accessed inside that page or post using the following object,
Here is the output of the same object for this page.
Filters are used to modify outputs. It is used along with objects. A simple example is shown below.
Here is the output for this post
Here the output string will be converted into uppercase letters.
I hope by now you have a basic idea of how liquid syntax works. But these are very simple examples. There can be complex liquid code blocks as well. Let’s discuss some variables that we can use in Jekyll.
Here is a link for all the filters that you can use: Liquid Filters
These are sitewide objects. They can be used anywhere on the website to fetch data either something related to the site or data from _config.yml
file.
Some examples here.
Variable | Description |
---|---|
{{site.time}} | Fetches current time - Ex: 2024-04-04 05:52:22 +0000 |
{{site.pages}} | Fetches a list of all the pages |
{{site.posts}} | Fetches a list of all the posts |
{{site.data}} | Fetches a list containing the data loaded from the YAML files located in the _data directory |
Site variables defined in _config.yml
are very useful and can be used to change things globally. Imagine you define your facebook username in the configuration file. You can access this from anywhere on the site.
Let’s say this is how you define all the social media accounts in _config.yml
,
Let’s say you create an author section with follow buttons,
Checkout the demo see the output.
This comes in very handy if you want to use these variables very often or change them altogether. I have used this feature while creating themes. Defining accent colors in configuration helps change the color globally.
Variable | Description |
---|---|
{{page.title}} | Fetches title of the page. Ex: - How to use Liquid Syntax in Jekyll? |
{{page.content}} | Fetches anything after frontmatter of the page |
{{page.excerpt}} | Fetches the excerpt or first paragraph of the page |
{{page.url}} | Fetches the url of the page. Ex: for this page - /amp/jekyll-liquid |
Page variables are page specific. They can only fetch data with respect to the page they are declared in.
These variables are very easy to implement but using them within a loop is a little tricky. I will give you some examples,
The output is here
I’m limiting the outputs to 3. If not, the whole page will be filled with titles. Here, pot
is just a variable. We generally use post
in its place. You can use any word that you fancy.
You can also fetch titles manually (not recommended!)
Here n
should be an integer. This method is not practical. You can use it if you’re willing to fetch the titles of first few posts.
For numbering a loop, you can use forloop.index
. This can also be used to provide different ids for consecutive elements in a for loop.
I have intentionally used pot
instead of post
because we can use any variable there.
The output looks like this,
The output is here
Since this page has a title that contains both Jekyll and Liquid, the output is as shown above.
Else if in Jekyll Liquid Syntax is implemented this way,
The output will be,
This is exactly how I have implemented active menu highlight for this blog. Navigate to About or Contact page and see how the colored-underline gets highlighted.
For loops
will usually have more than one output but if conditions
will have only one output.
Please go through this document to get familiar with other types of condtional flows.
Liquid: https://help.shopify.com/themes/liquid
Front Matter: http://jekyllrb.com/docs/frontmatter/
Templates: http://jekyllrb.com/docs/templates/
Watch the video: