6 Minutes to Build a Blog from Scratch with Eleventy
Table of Contents
Introduction
This tutorial will guide you through the process of building a blog from scratch using Eleventy, a simple and powerful static site generator. In just six minutes, you'll learn how to set up your project, create layouts, and manage collections, enabling you to launch a fully functional blog.
Step 1: Set Up Your Environment
To get started, make sure you have the following prerequisites:
- Node.js installed on your computer.
- A code editor (like Visual Studio Code).
Once these are in place, follow these steps:
-
Create a new project directory:
- Open your terminal or command prompt.
- Use the command to create a new folder:
mkdir my-blog cd my-blog
-
Initialize a new Node.js project:
- Run the following command and follow the prompts:
npm init -y
- Run the following command and follow the prompts:
-
Install Eleventy:
- Execute the command to install Eleventy as a development dependency:
npm install --save-dev @11ty/eleventy
- Execute the command to install Eleventy as a development dependency:
Step 2: Create Your First Blog Post
Now that you have Eleventy installed, you can create your first blog post.
-
Create a new directory for your posts:
- Inside your project folder, create a folder named
_posts
:mkdir _posts
- Inside your project folder, create a folder named
-
Create a Markdown file for a post:
- Inside the
_posts
directory, create a file namedmy-first-post.md
with the following content:--- title: My First Blog Post date: 2023-10-01 --- This is my first blog post using Eleventy!
- Inside the
Step 3: Configure Eleventy
Next, you need to set up your Eleventy configuration.
-
Create a configuration file:
- In your project root, create a file named
.eleventy.js
with the following content:module.exports = function(eleventyConfig) { return { dir: { input: "src", output: "dist" } }; };
- In your project root, create a file named
-
Create the necessary directories:
- Create a
src
directory for your templates and layouts:mkdir src
- Create a
-
Set up a layout:
- Inside the
src
directory, create a folder named_includes
and add a file calledlayout.njk
with the following content:<!DOCTYPE html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>{{ title }}</h1> <main> {{ content | safe }} </main> </body> </html>
- Inside the
Step 4: Build and Serve Your Blog
Now you can build and serve your blog locally.
-
Add a script to package.json:
- Open
package.json
and add the following script under "scripts":"scripts": { "start": "eleventy --serve" }
- Open
-
Run the server:
- Execute the following command in your terminal:
npm start
- Execute the following command in your terminal:
-
View your blog:
- Open a web browser and navigate to
http://localhost:8080
to see your blog in action.
- Open a web browser and navigate to
Conclusion
Congratulations! You've successfully built a blog from scratch using Eleventy. You can now explore further features, such as adding collections or integrating plugins like RSS. For more advanced configurations, refer to the documentation links provided in the video description. Happy blogging!