Creating a custom Jekyll theme allows you to personalize your website and improve its functionality. Using Liquid templates, you can design flexible and dynamic layouts that adapt to your content. This guide will walk you through the key steps to develop your own Jekyll theme from scratch.
Understanding Jekyll and Liquid
Jekyll is a static site generator that transforms plain text into static websites. It uses Liquid, a templating language, to insert dynamic content into your pages. Liquid allows you to include variables, loops, and conditional statements, making your theme highly customizable.
Setting Up Your Theme Directory
Start by creating a new directory inside your Jekyll site's _themes folder. Name it according to your theme, for example, my-custom-theme. Inside this directory, you'll add your layout files, includes, assets, and configuration files.
Creating the _layouts Folder
This folder contains the main templates for your pages. Create a file called default.html which will serve as the base layout.
Example content for default.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ page.title }} - {{ site.title }}</title>
<link rel="stylesheet" href="{{ "/assets/css/style.css" | relative_url }}">
</head>
<body>
<header>
<h1>{{ site.title }}</h1>
</header>
<main>
{{ content }}
</main>
<footer>
<p>© {{ site.time | date: "%Y" }} {{ site.author }}</p>
</footer>
</body>
</html>
Adding Includes and Assets
Includes are reusable snippets like navigation menus or footers. Create an _includes folder and add files such as nav.html or footer.html.
For assets like CSS, create an assets folder. Link your stylesheets and scripts in your layout files using Liquid filters, ensuring paths are relative.
Using Liquid in Your Templates
Liquid tags enable dynamic content rendering. For example:
- Variables: {{ page.title }}, {{ site.author }}
- Loops: {% for post in site.posts %} ... {% endfor %}
- Conditionals: {% if page.layout == "post" %} ... {% endif %}
Customizing Your Theme
Modify your layout files, add custom includes, and style your site with CSS. Use Liquid to insert dynamic data and create flexible templates that suit your content.
Publishing Your Theme
Once your theme is ready, package it by zipping the theme directory. Install it in your Jekyll site by adding it to the _config.yml under the theme key or by cloning it directly into the _themes folder.
Using a custom theme enhances your site's appearance and functionality. With Liquid templates, you can craft a unique, dynamic website tailored to your needs.