Nested Layouts for Clean Architecture

Why Layout Architecture Matters in Static Sites As your Jekyll site grows in complexity—adding blog posts, documentation, landing pages, and custom sections—your layout files can become bloated. Nested layouts allow you to separate concerns, improve reuse, and scale your HTML templates with a modular approach. The Concept of Layout Inheritance Jekyll allows layouts to inherit from other layouts using the layout key in the front matter of a layout file. This means you can define a base layout that handles your global HTML scaffolding, and then child layouts that insert their own structure or components. Example: Building a Modular Layout Hierarchy Imagine a site that has: A universal base layout with the doctype, head, and body wrapper A page layout for general pages A blog layout for articles with metadata and timestamps A docs layout that adds a sidebar and TOC Step 1: Create a Base Layout <!DOCTYPE html> <html lang="en"> <hea...

Using Data Files to Customize Jekyll 404 Pages

Why Use Data Files in 404 Pages

Most 404 pages are static by nature. But in a Jekyll-powered site, especially when hosted on GitHub Pages, you can bring in dynamic elements without JavaScript or server-side scripting by using data files. With _data folder support, you can design a maintainable, content-rich 404 page that auto-updates based on centralized content references.

Benefits of Data-Driven 404 Pages

  • Centralized Control: Update 404 page links and content by editing a single data file.
  • Reusability: Use the same data across multiple layouts (footer, sidebar, 404).
  • Scalability: Easily expand recommended content, categories, or resources over time.

Setting Up Your Data Files

Jekyll allows you to create files in the _data folder in formats like .yml, .json, or .csv. For a 404 page, YAML is preferred for simplicity.

Example: _data/404.yml

featured:
  - title: "Start Here Guide"
    url: "/getting-started/"
  - title: "Most Popular Posts"
    url: "/popular/"
  - title: "Explore Our Projects"
    url: "/projects/"
  - title: "About This Site"
    url: "/about/"

categories:
  - name: "Jekyll Basics"
    path: "/categories/jekyll/"
  - name: "Static Site SEO"
    path: "/categories/seo/"
  - name: "GitHub Hosting"
    path: "/categories/github-pages/"

Accessing Data in 404.html

Jekyll exposes data via the site.data object. Use Liquid to iterate through the values and inject them into your 404 page layout.

Example 404.html with Data-Driven Links

<div class="custom-404">
  <h2>Lost? Here's where you can go next.</h2>

  <h3>Recommended for You</h3>
  <ul>
    {% for item in site.data.404.featured %}
      <li><a href="{{ item.url }}">{{ item.title }}</a></li>
    {% endfor %}
  </ul>

  <h3>Browse by Topic</h3>
  <ul>
    {% for cat in site.data.404.categories %}
      <li><a href="{{ cat.path }}">{{ cat.name }}</a></li>
    {% endfor %}
  </ul>
</div>

Maintaining the Data File

To keep your 404 page current, regularly update the 404.yml file during content sprints. You don’t need to touch the HTML anymore—the links are auto-generated.

Tips for Maintenance

  • Use clear naming and avoid redundancy
  • Group content by user goals or intent
  • Sync updates with your content calendar

Optional: Add Descriptions and Icons

You can expand each data item with extra metadata like descriptions or icons. This allows a richer display on the 404 page.

Extended YAML Structure

featured:
  - title: "Start Here Guide"
    url: "/getting-started/"
    desc: "Learn how to use this site in 5 minutes"
    icon: "🧭"

Updated HTML

{% for item in site.data.404.featured %}
  <li>{{ item.icon }} <a href="{{ item.url }}">{{ item.title }}</a> - {{ item.desc }}</li>
{% endfor %}

Data-Driven Tag Cloud Example

Tags are harder to manage dynamically in Jekyll. But you can simulate a tag cloud by pre-defining your most-used tags in a data file and linking to their filtered pages.

Example _data/tags.yml

tags:
  - name: "liquid"
    count: 23
  - name: "seo"
    count: 19
  - name: "markdown"
    count: 14

404 Tag Cloud Display

<h3>Popular Tags</h3>
<ul>
  {% for tag in site.data.tags.tags %}
    <li style="font-size: {{ tag.count | divided_by: 5 | plus: 10 }}px;">
      <a href="/tags/{{ tag.name }}/">#{{ tag.name }}</a>
    </li>
  {% endfor %}
</ul>

Real-World Use Case

In one open-source documentation site built with Jekyll, we used a 404 page powered by three data files:

  • popular.yml – evergreen content and tutorials
  • tools.yml – most-used external resources
  • topics.yml – categories and docs links

This system allowed the documentation team to update content pointers during every release cycle without touching layout files. The result was a 40% drop in 404 exits over 3 months.

Pair With Client-Side Enhancements

You can use the data file to populate a dropdown menu or suggestions box powered by client-side JS. For example, show a "top tutorials" section that updates based on clicks or last week’s reads, loaded from a JSON data file.

Fallback Strategies

If the data file is not found or incorrectly formatted, Jekyll won’t render the page. So wrap your loops with conditionals:

{% if site.data.404 and site.data.404.featured %}
  {% for item in site.data.404.featured %}
    ...
  {% endfor %}
{% else %}
  <p>No suggestions available at the moment.</p>
{% endif %}

Conclusion

Using Jekyll data files to power your 404 page is a simple but powerful approach to maintain dynamic, helpful error pages without extra tools. It improves user experience, simplifies updates, and enhances the flexibility of your site architecture. Treat your 404 not as a mistake, but as an opportunity—automated, centralized, and smarter with every iteration.


Archives / All Content


© NetBuzzCraft🕒😃😃😃 . All rights reserved.