Getting Started with Eleventy
Eleventy (or 11ty) is a simple, static site generator designed to be an alternative to Jekyll. It’s written in JavaScript and allows you to create websites quickly by converting templates into static HTML. Here’s a step-by-step guide to getting started with Eleventy:
Prerequisites
- Basic knowledge of HTML, CSS, and JavaScript.
- Node.js and npm installed on your machine.
Step 1: Setting Up Your Project
-
Create a New Project Directory
mkdir my-eleventy-site cd my-eleventy-site
-
Initialize npm
npm init -y
-
Install Eleventy
npm install @11ty/eleventy --save-dev
Step 2: Create Basic Files
- Create a Basic HTML File
- Inside your project directory, create a folder named
src
. - In the
src
folder, create anindex.html
file with the following content:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Eleventy Site</title> </head> <body> <h1>Welcome to My Eleventy Site!</h1> </body> </html>
- Inside your project directory, create a folder named
Step 3: Configure Eleventy
- Create an Eleventy Configuration File
- Create a file named
.eleventy.js
in the root of your project directory with the following content:module.exports = function(eleventyConfig) { return { dir: { input: "src", output: "_site" } }; };
- Create a file named
Step 4: Build and Serve Your Site
- Add Scripts to
package.json
- Open your
package.json
and add the following scripts:"scripts": { "build": "eleventy", "serve": "eleventy --serve" }
- Open your
2. Build Your Site
bash npm run build
- This command will generate a `_site` directory with your static HTML files.
3. Serve Your Site
bash npm run serve
- This command will start a local development server. You can view your site by navigating to `http://localhost:8080` in your web browser.
Step 5: Using Templates and Data
Eleventy supports various templating languages such as Nunjucks, Liquid, Markdown, and more.
- Using Markdown
-
Create a
src/posts
folder and add a markdown filehello-world.md
:--- title: Hello World date: 2024-06-19 --- # Hello, World! This is my first blog post using Eleventy.
-
Eleventy will automatically process this markdown file and convert it to HTML.
-
- Using Layouts
-
Create a
src/_includes
folder. -
In the
_includes
folder, create a filebase.njk
(Nunjucks template):<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting started with Eleventy</title> </head> <body> <header> <h1>Getting started with Eleventy</h1> </header> <main> </main> </body> </html>
-
Update your
hello-world.md
to use this layout:--- title: Hello World date: 2024-06-19 layout: base.njk --- # Hello, World! This is my first blog post using Eleventy.
-
Step 6: Customize and Extend
- Explore more Eleventy features like collections, custom filters, shortcodes, and plugins to customize and extend your site’s functionality.
Resources
- Eleventy Documentation
- Eleventy GitHub Repository
With these steps, you should have a basic Eleventy site up and running. From here, you can explore more advanced features and customize your site to fit your needs.