Creating Custom Layouts in Jekyll: A Practical Tutorial

Techie     July 2024

Introduction

Jekyll is a popular static site generator that allows you to build simple, fast, and easy-to-maintain websites. One of its powerful features is the ability to create custom layouts, which helps maintain a consistent design across your site while keeping your code organized and DRY (Don’t Repeat Yourself). In this section, we’ll dive deeper into creating custom layouts in Jekyll, exploring the layout inheritance system, creating reusable layouts, and applying them to different pages. Let’s get started!


Understanding Layouts in Jekyll

In Jekyll, layouts are templates that define the structure of a page. By using layouts, you can separate the content of your site from its presentation, making your code cleaner and more maintainable. Jekyll uses the Liquid template engine, which allows you to include dynamic content in your layouts.


Creating a Basic Layout

To create a custom layout in Jekyll, follow these steps:

<!DOCTYPE html>
<html>
<head>
  <title>{ page.title }</title>
</head>
<body>
  <header>
    <h1>{ site.title }</h1>
  </header>
  <main>
    { content }
  </main>
  <footer>
    <p>&copy; { site.author } { "now" | date: "%Y" }</p>
  </footer>
</body>
</html>

In this example, we’re using Liquid tags to insert dynamic content. Creating Custom Layouts in Jekyll: A Practical Tutorial represents the title of the page, Techietuts represents the title of your site, and { content } will be replaced with the content of each individual blog post.

---
 layout: post
 title: "My Awesome Blog Post"
---


This is the content of my blog post.

The layout: post line in the front matter tells Jekyll to use the post.html layout for this specific blog post.


Layout Inheritance

Jekyll’s layout inheritance system allows you to create a hierarchy of layouts, making it easy to reuse common elements across different pages. Let’s extend our previous example by creating a layout for the homepage.

<!DOCTYPE html>
<html>
<head>
  <title>{ page.title }</title>
</head>
<body>
  <header>
    <h1>{ site.title }</h1>
  </header>
  <main>
    { content }
  </main>
  <footer>
    <p>&copy; { site.author } { "now" | date: "%Y" }</p>
  </footer>
</body>
</html>
---
layout: default
---

<article>
  <h2>{ page.title }</h2>
  { content }
</article>

Now, the post.html layout inherits the structure from default.html, and you only need to define the unique elements for blog posts.

---
title: "My Awesome Blog Post"
---

This is the content of my blog post.


Conclusion

Creating custom layouts in Jekyll is a powerful way to maintain a consistent design across your site and keep your code organized. By understanding layout inheritance and using reusable layouts, you can streamline your development process and focus on creating great content. Start experimenting with custom layouts in Jekyll, and you’ll discover how it enhances the flexibility and maintainability of your static site. Happy coding!


Thanks for reading, see you in the next one!