Khaled Garbaya

Engineering Leader, Software Developer & Educator

An introduction to start using Eleventy

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:

1npm install -g @11ty/eleventy 2echo '# Page header' > README.md 3eleventy 4

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

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 mylayout.njk.

Add the wrapping HTML code.

1--- 2title: My Website 3--- 4<!doctype html> 5<html lang="en"> 6 <head> 7 <meta charset="utf-8"> 8 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 9 <title>{{ title }}</title> 10 </head> 11 <body> 12 <!-- any page content that uses this layout goes here--> 13 {{ content | safe}} 14 </body> 15</html> 16

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.

1--- 2layout: mainlayout.njk 3title: My First Post 4tags: post 5--- 6 7## My First Post 8

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.

1[ 2 { 3 "name": "Pikachu", 4 "power": "Static" 5 }, 6 { 7 "name": "Charizard", 8 "power": "fire" 9 } 10] 11 12

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 pokemons
  • 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.

1--- 2pagination: 3 data: pokemons 4 size: 1 5 alias: pokemon 6permalink: "pokemons/{{ pokemon.name | slug}}/" 7tags: pokemonPage 8title: {{ pokemon.name }} 9--- 10<p> Name: {{ pokemon.name }} </p> 11<p> Power: {{ pokemon.power }} </p> 12

Where to go from here

Next one in your inbox

no spam only content