Update: I released a Course on 11ty and the jamstack. You can get it now by clicking here.
Eleventy, or 11ty, is a powerful yet straightforward static site generator. It does not require any config to get started. This what it will take you to get an 11ty project running:
Bootstrap an eleventy project
We'll start from an empty directory, initialize it as an npm package by calling
npm init -y. Install @11ty/eleventy,
npm i -D @11ty/eleventy, package and create the website entry point.
Eleventy can compile multiple file formats, HTML, markdown, liquid, and njk.
Eleventy Layouts are special templates that you can use to wrap other content. For more information, check the layouts docs.
Layouts will live in an _includes folder at the root of your project. For example, if you want to share a standard structure through all your blog posts page first, you need to create a file inside the _includes folder and give it a name; let's call it
Add the wrapping HTML code.
To use a layout, you need to specify the frontmatter
layout property inside the desired file. In this case, it will
post.md at the root level of the project.
Layouts in eleventy can use other layouts. You can also use a different template engine across layouts. This feature is convenient if we want to wrap some pages with extra markup but not all the pages.
Create pages from data in Eleventy
Eleventy supports a few data sources, JSON, and js files. Let's take the example of creating pages from a list of pokemon in a JSON file.
First, create a
_data folder at the root of your project, then create a
pokemons.json file inside the newly created folder.
Now create a file
pokemon-page.njk at the root of your project.
To make use of the data, you can use the frontmatter property
pagination. Pagination has some sub-props:
- data this can the name of the file inside the _data directory; in your case, it should be
- size where you can specify the size of the page it is usually 1
- alias, which will be the name of a current item in the list.
Every page will need a unique slug to construct the final URL. You can achieve that by using the permalink property. You can use the pokemon.name and pass it through the
slug eleventy filter.