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...

improving jekyll performance with html includes and layout reuse

Understanding Jekyll Performance Optimization

Performance is not just about load time. In Jekyll, performance also refers to build speed, file manageability, and how efficiently content is reused across pages. Leveraging includes and layout reuse is a foundational technique that improves development efficiency and site scalability.

What Are HTML Includes in Jekyll

Jekyll includes are reusable chunks of HTML code stored in the _includes directory. They allow you to modularize your templates and reuse content across multiple layouts or pages.

Benefits of Using Includes

  • Reduces Duplication: Shared sections like headers, footers, and ads are maintained in one place.
  • Improves Maintainability: Changes in one include file update all references site-wide.
  • Better Build Speed: Modular code often compiles more predictably and quickly.

How to Create and Use Includes

Create a file inside _includes, for example, cta-box.html. Then call it in any layout or page using:

{% raw %}{% include cta-box.html %}{% endraw %}

Includes can accept parameters as well:

{% raw %}{% include button.html label="Download PDF" url="/docs/file.pdf" %}{% endraw %}

Example of Parameterized Include


{{ include.label }}

Layout Inheritance and Reuse

Jekyll uses layout files to define the HTML structure around your content. These are stored in the _layouts folder. You can nest layouts by calling a base layout inside a more specific one using the layout property in the front matter.

Example: Nested Layouts




  {% raw %}{% include head.html %}{% endraw %}
  
    
{% raw %}{% include header.html %}{% endraw %} {{ content }} {% raw %}{% include footer.html %}{% endraw %}

---
layout: base
---

{{ page.title }}

{{ content }}

Best Practices for Includes and Layouts

  • Name includes clearly: Use descriptive names like post-meta.html or nav-mobile.html.
  • Keep includes small: Each include should do one thing well. Don't make includes that span the entire page.
  • Organize includes in subfolders: For example, _includes/navigation/top.html.
  • Use comments sparsely: In production, comments increase output size. Avoid them unless necessary during dev.

Case Study: Rebuilding a Blog with Includes

A personal finance blogger rebuilt their Jekyll blog by modularizing recurring components—opt-in forms, banners, social buttons—into includes. Over 45 pages were reduced to simple markdown files with reusable elements, decreasing build times by 30% and editing time by 60%.

Improving Performance Through Smart Reuse

Here are specific ways includes and layout reuse improve performance:

Faster Builds

Smaller, modular files mean less processing. Pages reference includes rather than repeating content.

Less Code Duplication

Reusable layouts avoid rewriting entire HTML scaffolding for each content type.

Consistent Design

Every change to an include or layout cascades site-wide. This consistency enhances UX and branding.

Advanced Include Strategies

Advanced Jekyll setups use conditional logic in includes, such as displaying different banners based on categories, or loading external data using YAML or JSON.

{% raw %}{% if page.category == "guides" %}
  {% include banner-guide.html %}
{% endif %}{% endraw %}

Conclusion

Using HTML includes and layout inheritance in Jekyll is more than a productivity hack—it’s a core method to build scalable, maintainable, and high-performance websites. Whether managing a blog with 20 pages or a documentation site with hundreds, modular architecture is key to long-term success.


Archives / All Content


© NetBuzzCraft🕒😃😃😃 . All rights reserved.