Introduction to Eleventy, a Modern Static Website Generator
We show how Eleventy provides a nice flowing web dev process that works with existing technologies while steering you into good practices.
Jan 13th, 2024 6:00am by
Photo by Marcel Eberle on Unsplash
> node --version
v18.15.0
> npm install @11ty/eleventy --save-dev
added 214 packages, and audited 215 packages in 18s
echo '<!doctype html><title>Page title</title><p>Hi</p>' > index.html
echo '# Page header' > README.md

Running 11ty
So it treated the output of my README.md file as a new path, with its own default index page. It also seemed to use Liquid, a template language, to work out what to do with the files.
So let’s look at the site in a local server:
npx @11ty/eleventy --serve
[11ty] Writing _site/README/index.html from ./README.md (liquid)
[11ty] Writing _site/index.html from ./index.html (liquid)
[11ty] Wrote 2 files in 0.51 seconds (v2.0.1)
[11ty] Watching…
[11ty] Server at http://localhost:8080/
As expected, we get a page at http://localhost:8080 with a solitary “Hi” and a page at http://localhost:8080/README with the words “Page Header” wrapped in <h1>. You can leave this process to “hot load” new pages, and Ctrl-C when you want to restart.
So getting the basics up and running is easy, and the results are a sensible output structure. And this was all from the guide. (We could also have made an input source directory).
Template Languages and Front Matter
Now for the interesting stuff. For a personal website, we will want our pages to share a layout. We want to keep our content in Markdown, and let Eleventy make the site for us. There are two useful concepts that Eleventy uses to do this. Template languages allow you to intersperse your intended output language (HTML for a website) with code instructions. We generally need to differentiate between lines that are “this is code, just leave it be” and lines that are “put the result of this on the screen.” As we saw, Eleventy uses the Liquid template by default. So here is some simple Liquid, that shows all the bits:
{% if username %}
Hello <b>{{ username }}</b>!
{% endif %}
<!doctype html>
<title>Page title</title>
{% assign username = "David"%}
{% if username %}
Hello <b>{{ username }}</b>!
{% endif %}

A bit of Liquid
---
username: "David"
---
<!doctype html>
<title>Page title</title>
{% if username %}
Hello <b>{{ username }}</b>!
{% endif %}
Creating the Website
OK, so let’s get back to our website. A reminder:- We want pages of our website to use a layout.
- But we only want to write content in Markdown, not hack through HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nice Cat Page</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f8f8f8;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: white;
padding: 10px;
}
img {
max-width: 100%;
height: auto;
border-radius: 8px;
margin-top: 20px;
}
footer {
background-color: #333;
color: white;
padding: 10px;
position: fixed;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<header>
<h1>Nice Cat Page</h1>
</header>
<img src="https://placekitten.com/800/400" alt="A nice cat">
<footer>
<p>© 2024 Nice Cat Page. All rights reserved.</p>
</footer>
</body>
</html>
But we want this as our layout page. We want the actual title of our page to appear where “Nice Cat Page” appears, and we want to put some actual text content just under the cat.
So let’s rename this cat page layout.html, and alter it to put in Liquid template variables for what we want. Here…
..
<header>
<h1>{{ title }}</h1>
</header>
..
..
<img src="https://placekitten.com/800/400" alt="A nice cat">
{{content}}
<footer>
<p>© 2024 Nice Cat Page. All rights reserved.</p>
</footer>
..
As there is no opening index.html, nothing will be served. All we have is the old README.
So let’s make an index.md, tell it about the title and content, and the layout. We use front matter for this:
---
layout: "layout.html"
title: "Davids Website"
---
Hi there, I like cats!!

New kitten, new danger
This should be enough to stir some enthusiasm in you to write your own site, or perhaps be enough to put you off. However, you will need to go deeper than this to get all the nice components of a modern site, and I will do this in a later post. The takeaway, for now, is that Eleventy gives us a nice flowing process that works with existing technologies while steering us into good practices.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.