Eleventy (or 11ty) is a simple, yet powerful static site generator built on JavaScript. It allows you to build static websites from templates and content files such as Markdown, HTML, YAML, JSON, etc. Here’s a step-by-step tutorial to get you started with Eleventy:
First, ensure you have Node.js and npm installed on your system. Then, you can install Eleventy globally or locally within your project:
npm install -g @11ty/eleventy
Create a new directory for your Eleventy project and navigate into it:
mkdir my-eleventy-project
cd my-eleventy-project
Initialize your project with npm to create a package.json
file:
npm init -y
Eleventy supports multiple formats for content files, including Markdown, HTML, YAML, JSON, etc. Create your content files in the source directory. For example, you can create an index.md
file:
---
title: My Eleventy Website
---
# Welcome to Eleventy
This is a simple Eleventy tutorial.
Create templates for your website. Eleventy uses template engines like Nunjucks, Liquid, Handlebars, etc. Create a layout file, for example, layouts/base.njk
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
</head>
<body>
<header>
<h1>{{ title }}</h1>
</header>
<main>
{{ content | safe }}
</main>
</body>
</html>
Create a configuration file, typically named .eleventy.js
, to configure Eleventy:
module.exports = function(eleventyConfig) {
eleventyConfig.addPassthroughCopy("assets"); // Copy assets folder to output
return {
dir: {
input: "src", // Input directory
output: "dist", // Output directory
includes: "_includes" // Layouts and includes directory
}
};
};
Run Eleventy to build your website:
npx eleventy
This command will process your source files, apply templates, and generate the output in the specified output directory (dist
in this case).
Explore the generated output in the dist
directory to see your transformed files.
You can customize your Eleventy setup by configuring additional options in the .eleventy.js
file, adding filters, and using plugins as needed.
You can automate the build process using npm scripts or tools like Gulp or Grunt. Define scripts in your package.json
file to run the Eleventy build command.
That’s a basic overview of getting started with Eleventy. From here, you can explore its extensive features, such as collections, pagination, data files, and more, to build powerful static websites.