Let's Learn 11ty Part 1: Installation & Setup

Let's Learn 11ty Part 1: Installation & Setup

Read 2532 times

#webdev

#tutorial

#ssg

In this article, I said that we will be doing a complete tutorial of Eleventy - from scratch to deployment. Before we get into that, let’s talk a bit more about Eleventy.

How Eleventy Works

Eleventy aims to ship as little javascript as possible. It does this by rendering your source files into html and serving those static files.

Templates

Templates is the way you create your various pages in Eleventy. It supports over 3 template languages. Some of them notably:

What this means is that you have several options to how you choose to author your site. In this tutorial, we will be using markdown and nunjucks for our templates.

Requirements

At this stage, you’ll need only 3 things:

  1. Node
  2. A text editor
  3. Willingness to learn p

Make sure you have node installed , then continue with this tutorial


Setup

  1. Create an empty folder anywhere on your computer and navigate into it
mkdir learneleventy
cd  learneleventy
  1. Initialise an empty package.json file with
npm init -y
  1. Install eleventy
npm install --save-dev @11ty/eleventy

Configure Project

By default Eleventy uses files your project root to generate your site - you could place all your files there and it would work. I prefer a more ordered approach - having an src folder. Luckily for us Eleventy allows us to modify the path where it looks for source files

In your project root, create a file called .eleventy.js and place this in it:

module.exports = function(eleventyConfig){
  return {
    dir: {
      input: "src",
      data: "_data",
      includes: "_includes",
      layouts: "_layouts"
    }
  };
}

This is our configuration file for our project. In it we define how we want a our project to behave. For now:

With that done, we make an src in our root and that’s where our files will go. Let’s make two more folders in src that we will use later: _layouts and _includes

Let’s make a index.md file in the root of our src and lets put some dummy content

# My Eleventy Site

I am a site made with Eleventy

Running Our Project

To run our site, type this command in your terminal

npx eleventy --serve

Your site will be running in development on http://localhost:8080/. You will also notice that a _site folder has been generated, this is where our rendered files will go.

Eleventy Site

We don’t want to always have to write this when we run our project, so let’s add a script in package.json

 "scripts": {
    "start": "eleventy --serve"
  },

Gotcha

If we were to change anything in our index.md, those changes would not show unless reloaded the page. Eleventy requires a valid html document structure to facilitate that.

Your First Template

Let’s now implement templates in our Eleventy project. In src/_layouts make a file called base.njk

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My Eleventy Site</title>
  </head>
  <body>
    {{content | safe}}
  </body>
</html>

As you can see it’s just an html document. The thing that’s different is {{content | safe}}. This is where any content wrapped by our base will go.
The | safe part is a filter that tells Eleventy that the content is safe to render.

Then modify index to look like this:

---
layout: base
---

# My Eleventy Site

I am a site built with [Eleventy](https://www.11ty.io/).

We added YAML frontmatter and specify the layout that is supposed to wrap this file.

Now any changes we make to our source files will automatically update. Also, our page now has a title and doesn’t say localhost:8080

NOTE : You can override the template languages in your project if you want to use anything other than markdown an nunjucks.

Using Variables In Templates

Usually each page on a site has a unique title. We have hardcoded the title. Let’s improve that so that the page title is applied dynamically.

Modify our base with this

 <title>My Eleventy Site - {{title}}</title>

Then our index.md YAML with thiis

---
layout: base
title: Home
---

Now when we hover over the tab we see My Eleventy Site - Home


Conclusion

Without doing a lot, we have built a website. It may not look nice right now, but it is valid webpage.

Join me in the next part as we add more to this site.

The working repository for this series can be found here: Part 1: Installation

Back to articles