jekyll data files for global site control
What Are Jekyll Data Files
Jekyll data files are YAML, JSON, or CSV files stored in the _data folder that hold structured data. This data can be accessed globally across layouts, pages, posts, and includes. It’s a powerful feature for centralizing configuration, managing repeatable content, or generating dynamic content sections without duplicating code.
Why Use Data Files in Jekyll Projects
Data files provide a clean separation between content and logic. Instead of hardcoding information across multiple files, you define it once and reuse it. This improves maintainability, scalability, and reduces the chance of errors when updates are needed.
Setting Up Jekyll Data Files
Step 1: Create the _data Folder
By default, Jekyll looks for data files inside a directory named _data in the root of your project.
mkdir _data
Step 2: Add a YAML File
Let’s say you want to manage your site’s navigation. Create a file called navigation.yml in _data:
- title: Home url: / - title: Blog url: /blog/ - title: Projects url: /projects/ - title: About url: /about/
Step 3: Load Data in a Layout or Include
Use Liquid to loop through the data in your layout:
{% raw %}
-
{% for item in site.data.navigation %}
- {{ item.title }} {% endfor %}
Use Cases for Jekyll Data Files
1. Navigation Menus
As shown above, you can manage primary and secondary menus from a single source of truth.
2. Team or Staff Directory
Create a team.yml file and loop through members on the about page or a custom staff page.
- name: Alice role: Lead Developer photo: /images/alice.jpg - name: Bob role: UI Designer photo: /images/bob.jpg
3. Pricing Tables
Update pricing packages without touching HTML pages directly.
4. FAQs and Reusable Snippets
Maintain frequently asked questions or commonly used content blocks like product features.
Real-World Example: Multi-Language Site Navigation
A bilingual company site uses two files in _data—navigation_en.yml and navigation_fr.yml. Depending on the user’s language preference (detected via URL prefix), the site loads the appropriate navigation dynamically. This helped the team maintain consistency without duplicating layout logic.
Best Practices for Managing Data Files
- Use YAML format for readability unless you need arrays or deeply nested structures, where JSON may be clearer.
- Break large datasets into multiple files for modularity (e.g., separate navigation, team, pricing).
- Use consistent keys across entries to simplify access and avoid errors.
- Validate syntax before pushing changes—incorrect indentation can break your build.
Dynamic Includes Using Data
You can use includes that dynamically pull from data. For example, an include like feature-box.html might use values passed from a loop:
{% raw %}
{% for feature in site.data.features %}
{% include feature-box.html title=feature.title icon=feature.icon %}
{% endfor %}
{% endraw %}
Combining Data Files with Collections
While collections define custom content types, data files can supplement them with external metadata. For example, a podcast collection can pull episode metadata from episodes.yml to enrich layouts.
Limitations to Be Aware Of
- Data files are read-only at build time; they don’t change dynamically like a CMS.
- Large or complex datasets can slow down the build process if not structured efficiently.
- There is no native UI—editing is done directly in the file, requiring careful syntax.
Conclusion
Jekyll data files are essential for creating modular, scalable, and maintainable static websites. They reduce redundancy and offer developers a powerful way to manage content across pages. When used effectively, they make your Jekyll site more dynamic and easier to update—without ever touching your layout files.
