Static Sites with Eleventy
By the end of this lesson you will be able to build a static site with Eleventy: configure it, feed it a global data file, write a Nunjucks layout, add a date filter, create one page per event with pagination, and run its dev server. You will also know how Eleventy and Astro differ, and when to pick each.
What you should know first: Set Up Your Jamstack Toolkit and Static Sites with Astro. We rebuild the same meetup pages in a separate folder, rohtak-meetup-11ty, so you can compare the two tools side by side. The main course project stays in Astro. Version used: Eleventy 3.1.8 (the current stable release; version 4 is still in alpha) on Node.js 26. Eleventy 3 needs Node.js 18 or newer. All outputs are from real runs.
What Eleventy is
Eleventy (also written 11ty) is a static site generator that turns templates and data into HTML. It has no front-end framework and adds no JavaScript to your pages. It supports many template languages: HTML, Markdown, Nunjucks, Liquid, JavaScript and more.
If Astro is a modern modular kitchen with many built-in machines, Eleventy is a plain, well-organised kitchen where you choose every tool yourself. Less is decided for you, which makes it simple to learn and very fast for content sites, but you add features such as image processing through plugins.
Step 1: create the project
mkdir rohtak-meetup-11ty
cd rohtak-meetup-11ty
npm init -y
npm install @11ty/eleventyadded 127 packages, and audited 128 packages in 1m 42 packages are looking for funding run `npm fund` for details found 0 vulnerabilities
npm init -y writes a basic package.json with "type": "commonjs". We want modern ES modules and two short scripts, so set them with npm pkg:
npm pkg set type=module scripts.dev="eleventy --serve" scripts.build="eleventy"Step 2: the folder layout and config
By default Eleventy treats the project folder itself as input. Keeping source files in src/ is tidier and stops Eleventy from looking at node_modules/ and package.json. This is the layout we build:
rohtak-meetup-11ty/
├── eleventy.config.js
├── package.json
└── src/
├── _data/
│ └── events.json global data: available as "events" everywhere
├── _includes/
│ └── base.njk the layout
├── css/
│ └── style.css
├── events/
│ ├── index.njk /events/
│ └── event.njk one page per event (pagination)
└── index.md the home pageEleventy looks for a config file named eleventy.config.js (older projects use .eleventy.js). Create it in the project root:
const dateFormat = new Intl.DateTimeFormat("en-IN", {
dateStyle: "medium",
timeZone: "Asia/Kolkata",
});
export default function (eleventyConfig) {
// Copy the CSS folder to the output as it is
eleventyConfig.addPassthroughCopy("src/css");
// {{ event.date | readableDate }} gives "12 Dec 2026"
eleventyConfig.addFilter("readableDate", (isoDate) =>
dateFormat.format(new Date(`${isoDate}T00:00:00+05:30`))
);
}
export const config = {
dir: {
input: "src",
output: "_site",
},
};Three ideas live here. Passthrough copy copies files such as CSS and images to the output without processing them. A filter is a small function you can apply in a template with the | symbol, like a pipe in a terminal. The exported config object moves the input folder to src/; the output folder _site is Eleventy's default. The _includes and _data folder names are defaults too, and are found inside the input folder.
Step 3: global data and a layout
Copy the same events.json from the Astro project into src/_data/events.json. Any JSON file in _data/ becomes a variable named after the file, so every template can now use events. No import is needed.
The layout is a Nunjucks template, a template language with {{ value }} for output and {% tag %} for logic. Create src/_includes/base.njk:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="{{ description }}">
<link rel="stylesheet" href="/css/style.css">
<title>{{ title }} | Rohtak Tech Meetup</title>
</head>
<body>
<header>
<a href="/">Rohtak Tech Meetup</a>
<nav aria-label="Main">
<a href="/">Home</a>
<a href="/events/">Events</a>
</nav>
</header>
<main>
{{ content | safe }}
</main>
</body>
</html>{{ content }} is the page's rendered body, the same job as <slot /> in Astro. It is marked safe because it is HTML that Eleventy has already produced from your template. safe means "do not escape this", so use it only for content like this, never for outside data.
Step 4: pages that use the layout
The home page is Markdown with front matter, the YAML block between --- lines that sets data for one page. Create src/index.md:
---
layout: base.njk
title: Home
description: A free monthly meetup for developers and students in Rohtak.
---
# Rohtak Tech Meetup
Learn, share and build with developers and students in Rohtak.
The next session is **{{ events[0].title }}** on {{ events[0].date | readableDate }}.
[See all events](/events/)Eleventy first runs Markdown files through a template language (Liquid by default), then turns the Markdown into HTML. That is why {{ }} works inside Markdown. Next, the list page src/events/index.njk:
---
layout: base.njk
title: Events
description: All upcoming Rohtak Tech Meetup sessions.
---
<h1>Upcoming events</h1>
<ul>
{%- for event in events %}
<li>
<a href="/events/{{ event.slug }}/">{{ event.title }}</a>,
{{ event.date | readableDate }}, {{ event.speaker }}
</li>
{%- endfor %}
</ul>The - in {%- removes the whitespace before the tag, which keeps the output tidy.
Step 5: one page per event with pagination
Astro used getStaticPaths(). Eleventy uses pagination: front matter that tells one template to repeat over a data set. Create src/events/event.njk:
---
layout: base.njk
pagination:
data: events
size: 1
alias: event
permalink: "/events/{{ event.slug }}/"
eleventyComputed:
title: "{{ event.title }}"
description: "{{ event.summary }}"
---
<h1>{{ event.title }}</h1>
<dl>
<dt>When</dt>
<dd>{{ event.date | readableDate }}, {{ event.startTime }}</dd>
<dt>Where</dt>
<dd>{{ event.venue }}</dd>
<dt>Speaker</dt>
<dd>{{ event.speaker }}</dd>
</dl>
<p>{{ event.summary }}</p>
<p><a href="/events/">All events</a></p>data: events names the array, size: 1 puts one item on each page, and alias: event gives that item a friendly name. permalink decides the output URL from the item's slug. eleventyComputed sets the title and description per event, so the layout's <title> is correct on each page.
Pagination is just "cut the list into chunks and make a page per chunk". The script below does the same with size: 1 and with size: 2 (a listing with two events per page):
// What Eleventy pagination does with `data`, `size` and `permalink`
const events = [
{ slug: "astro-basics-dec-2026", title: "Fast Websites with Astro" },
{ slug: "git-for-beginners-jan-2027", title: "Git for Absolute Beginners" },
{ slug: "accessible-forms-feb-2027", title: "Accessible Forms in Plain HTML" },
];
function paginate(items, size) {
const pages = [];
for (let i = 0; i < items.length; i += size) {
pages.push(items.slice(i, i + size));
}
return pages;
}
// size: 1 with alias "event" -> one page per event
for (const [event] of paginate(events, 1)) {
console.log(`/events/${event.slug}/`);
}
// size: 2 -> listing pages with two events each
paginate(events, 2).forEach((chunk, pageNumber) => {
const url = pageNumber === 0 ? "/events/" : `/events/page-${pageNumber + 1}/`;
console.log(url, chunk.map((e) => e.title).join(" + "));
});/events/astro-basics-dec-2026/ /events/git-for-beginners-jan-2027/ /events/accessible-forms-feb-2027/ /events/ Fast Websites with Astro + Git for Absolute Beginners /events/page-2/ Accessible Forms in Plain HTML
Step 6: build and serve
npm run build[11ty] Writing ./_site/events/index.html from ./src/events/index.njk [11ty] Writing ./_site/events/astro-basics-dec-2026/index.html from ./src/events/event.njk [11ty] Writing ./_site/index.html from ./src/index.md (liquid) [11ty] Writing ./_site/events/git-for-beginners-jan-2027/index.html from ./src/events/event.njk [11ty] Writing ./_site/events/accessible-forms-feb-2027/index.html from ./src/events/event.njk [11ty] Copied 1 Wrote 5 files in 1.63 seconds (326.8ms each, v3.1.8)
Five pages, the same as Astro, plus one copied CSS file. Note "(liquid)" next to index.md: that is the template language used inside the Markdown. Here is the body Eleventy wrote for the Git event:
<h1>Git for Absolute Beginners</h1> <dl> <dt>When</dt> <dd>9 Jan 2027, 11:00</dd> <dt>Where</dt> <dd>Model Town Community Hall, Rohtak</dd> <dt>Speaker</dt> <dd>Kabir Hooda</dd> </dl>
Unlike Astro, Eleventy does not minify or rewrite your HTML. The output keeps your own indentation, which makes it easy to read. For editing, start the dev server:
npm run dev[11ty] Copied 1 Wrote 5 files in 1.65 seconds (330.3ms each, v3.1.8) [11ty] Watching… [11ty] Server at http://localhost:8080/ [11ty] File changed: ./src/_data/events.json [11ty] Copied 1 Wrote 5 files in 0.41 seconds (v3.1.8)
We changed the seats in events.json while it was running, and Eleventy rebuilt every page that uses events in 0.41 seconds. The dev server also adds a small reload script to each page (/.11ty/reload-client.js) so the browser refreshes itself. That script is not in the files npm run build writes.
Escaping: the template language matters
In Astro, every { } is escaped. In Eleventy, it depends on the template language. We built three tiny pages that print the same data value, <b>Meera</b> & co:
NJK: <b>Meera</b> & co LIQ: <b>Meera</b> & co <p>MD: <b>Meera</b> & co</p>
Nunjucks escaped it. Liquid printed the tags as real HTML, and so did Markdown (which runs Liquid first). For your own JSON file that may be fine. For data from a headless CMS, a form or a third-party API, it is an XSS risk. Use Nunjucks for templates that print outside data, or apply Liquid's escape filter, as in {{ comment.text | escape }}. Also remember that Markdown allows raw HTML, so do not build pages from Markdown that untrusted people wrote without sanitising it.
Eleventy or Astro?
| Question | Eleventy 3 | Astro 7 |
|---|---|---|
| Templates | Nunjucks, Liquid, Markdown, JS and more | .astro components (plus React, Vue and others) |
| JavaScript sent to visitors | None unless you add it | None unless you add a <script> or island |
| Output HTML | As you wrote it | Compiled and minified |
| Scoped CSS, script bundling | Through plugins | Built in |
| Interactive islands | Add your own scripts | client: directives built in |
| On-demand pages | Not its focus | With an adapter |
| Learning curve | Small, plain files | Small, with more features to learn |
Pick Eleventy for content-heavy sites such as documentation, blogs and small organisation sites, especially when the team likes plain templates and full control of the HTML. Pick Astro when you want components, scoped styles and islands built in, or may need a few server-rendered pages later. This course continues with the Astro project; the Eleventy version is your reference for the documentation site project, where Eleventy is a good fit.
Common mistakes
- Forgetting
| safeoncontentin a layout. The page's HTML then shows up as escaped text with visible tags. - Using
| safeon outside data. That switches off escaping exactly where you need it. - Leaving the input as the project root. Without
dir.input, files such asREADME.mdbecome pages. - Expecting CSS to appear by itself. Files that are not templates need
addPassthroughCopy. - Liquid for untrusted data. Liquid does not escape by default; use Nunjucks or the
escapefilter.
Interview questions
1. How does Eleventy make one page per item in a data file?
With pagination in the template's front matter: pagination.data names the data, size: 1 puts one item on each page, alias names the item, and permalink builds each URL from the item's fields.
2. What is the _data folder for?
Files in it become global data. _data/events.json is available in every template as events, without any import. JavaScript files there can also fetch data at build time.
3. Why must content be marked safe in a Nunjucks layout?
Nunjucks escapes output by default, and content is already rendered HTML from the page. Marking it safe prints it as HTML. The same filter must not be used on untrusted values, because it disables escaping.
4. When would you choose Eleventy over Astro?
For content-focused sites where plain templates, unmodified HTML output and minimal tooling matter more than components and built-in islands, such as documentation or a small blog. Astro suits sites that need components, scoped CSS, interactive islands or a few on-demand pages.
Key points
- Eleventy 3 turns templates and data into HTML and adds no JavaScript to pages.
eleventy.config.jssets passthrough copies, filters and the input and output folders.- JSON in
_data/becomes global data; layouts live in_includes/and print{{ content | safe }}. - Pagination with
size: 1,aliasandpermalinkcreates one page per data item. - Nunjucks escapes output by default; Liquid and Markdown do not, so be careful with outside data.
- Eleventy suits plain content sites; Astro adds components, scoped CSS and islands.
Exercise
Add a page per speaker to the Eleventy version. Create src/speakers/speaker.njk that uses pagination over events to build /speakers/<slug>/, where the slug comes from the speaker's name using Eleventy's built-in slugify filter. Each page should show the speaker's name and a link to their talk. Build the site and check that the log reports 8 files written.
Show solution
---
layout: base.njk
pagination:
data: events
size: 1
alias: event
permalink: "/speakers/{{ event.speaker | slugify }}/"
eleventyComputed:
title: "{{ event.speaker }}"
description: "Speaker at the Rohtak Tech Meetup."
---
<h1>{{ event.speaker }}</h1>
<p>Talk: <a href="/events/{{ event.slug }}/">{{ event.title }}</a>,
{{ event.date | readableDate }}</p>slugify turns "Kabir Hooda" into kabir-hooda, so the page is written to _site/speakers/kabir-hooda/index.html. Three speaker pages plus the five earlier pages give "Wrote 8 files". The template is Nunjucks, so the names stay escaped. If two talks ever share a speaker, this simple version would try to write the same URL twice and Eleventy would stop with an error, which is a hint to build a separate speakers data list later.
Quick Quiz
1. Where must events.json go so every Eleventy template can use events?
JSON files in the data folder become global data named after the file.
2. In Eleventy pagination, what does size: 1 do?
size is the number of items on each generated page.
3. Which line prints a value from outside data without escaping?
Liquid does not escape by default in Eleventy; Nunjucks does.
4. Why is addPassthroughCopy("src/css") needed?
Without it, Eleventy ignores the CSS folder and the stylesheet link returns 404.
Learn Jamstack with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
