Jekyll Liquid basics
Tags: jekyll, jamstack, beginners, tutorial
Published: 2022-06-10 19:59:00

---
canonical_url: https://cloudcannon.com/community/learn/jekyll-tutorial/introduction-to-liquid/
series: Jekyll Beginner Tutorial
--- 
By [Farrel Burns](https://cloudcannon.com)

Brought to you by [CloudCannon](https://cloudcannon.com/jekyll-cms/), the Git-based CMS for Jekyll.

#### What you’ll learn here:

* The basics of Liquid
    
* Using filters to modify content
    
* Writing conditional statements
    
* Iterating through data arrays
    

```shell
    # Starting repo
    git clone https://github.com/CloudCannon/jekyll-learn-blog-template.git
    
    # Starting branch:
    git checkout liquid-intro-start
    
    # Finished branch:
    git checkout liquid-intro-finish
```

What is Liquid?
---------------

Liquid is a templating language used in Jekyll to process your site’s pages. In other words, it helps you make your HTML pages a bit more dynamic, for example adding logic or using content from elsewhere. This doesn’t require any setup - we can just start using it.

From this lesson on, you can find a link to GitHub repositories with a project that will help you practice Jekyll and Liquid - both before and after the lesson. Feel free to clone the project and follow along. Read more on [Liquid on Jekyll’s official site](https://jekyllrb.com/docs/liquid/).

Liquid basics
-------------

To use Liquid in your pages, you first need pages to have “front matter” notation (our next lesson - don’t worry for now) at the top of our page:

```plaintext
    -—-
    -—-
    <!DOCTYPE html>
```

For Liquid code itself, there are two types of tags:

*   For outputting content into a page, use two curly brackets on each side: `{{ content }}`
*   For logic/code, use a curly brackets and % sign on each side: `{% if condition == true %}`

Importantly, logic/code blocks must also have an “end” statement, for example `{% endif %}, {% endfor %}`

You can also set variables on pages:

*   `{% assign myVariableName = Content of my variable; %}`

Our next lesson - front matter - makes variables even easier, but you will often still find uses for Liquid variables (such as filtering front matter).

### Filters

A filter is something that you can use to change strings (text) or manipulate arrays (lists of items). To use a filter, separate the content you want to filter with a `|` sign and use a filter keyword. Filters can also be chained and take “arguments” - that is, extras to modify output more specifically.

There are many, many filters available that you might want to learn about (see our cheat sheet for more), but here we’ll just look at some common examples and output:

All uppercase

*   `{{ "uppercase" | upcase }}` = UPPERCASE

All lowercase

*   `{{ "LOWERCASE" | downcase }}` = lowercase

Length of a string

*   `{{ "How long am I?" | size }}` = 14

Add two pieces of text together (with argument)

*   `{{ "Copyright " | append: "My Blog" }}` = Copyright My Blog

Simple date formatting - international format

*   `{{ "2021-01-01T00:00:00Z" | date_to_long_string }}` = 01 January 2021

### Conditions

A condition is a great way to display content on your page based on decisions. These also combine well with “logical operators” for making comparisons:

| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| and | Both condition A and B |
| or | Either condition A or B |


The most common type of condition in Liquid is the “if” statement. Here’s an example of displaying a title depending on a title variable:

```html
    {% assign title = "home" %}
    {% if title == "home" %}
      <h1>Welcome to my homepage!</h1>
    {% endif %}
```

You can also use additional conditions - `elsif` (else if) and `else` - in each block:

```html
    {% assign title = "home" %}
    {% if title == "home" %}
      <h1>This is the homepage</h1>
    {% elsif title == "about" %}
      <h1>This is the about page</h1>
    {% else %}
      <h1>Welcome!</h1>
    {% endif %}
```

### Loops

A loop makes it easier to work with arrays. Here in NZ, we love our strange and characterful birds, so let’s list some on our page. Normally, in HTML, we would have to manually enter content like this:

With a loop, we can make this easier and more dynamic. The syntax for a loop is `for <variable> in <list of items>`, where “variable” can be anything you choose:

```html
    {% assign products = "Kiwi,Tui,Kea,Karariki,Weka" | split: "," %}
    <ul>
      {% for item in products %}
        <li>{{ item }}</li>
      {% endfor %}
    </ul>
```

Now we can add extra items to our products array above and it will automatically appear in the HTML because of our loop. You can even use `if` statements inside loops for even more control.

Whats next?
-----------

Now we know a bit of how Liquid works. There’s plenty more to learn, as it’s a big topic, so we’ve [prepared a cheat sheet](https://learn.cloudcannon.com/jekyll-cheat-sheet/) for you too! Another question worth answering is where we get our data from that we can use with Liquid. The array example above isn’t a great solution. In our next topic - front matter - we’ll deal with this and learn how to make our pages even more powerful.