How To Build a Static Blog with 11ty

Written by daily-dev-tips | Published 2020/04/21
Tech Story Tags: how-to-make-a-static-website | static-site-generator | static-website | blogging | netlify | daily-dev-tips | tutorial | web-development | web-monetization

TLDR Eleventy (11ty) is a static site generator (SSG) called a simple blog generator. It's super simple to get started with. The main reason for me to use 11ty is that it's so fast and customizable. It's so easy to create a simple website/blog with a basic node application. We can now start working on adding some pages. We create a homepage, create a postlist and add an essential back to home button. We then create a layout for this file up top of this file.via the TL;DR App

Ever wondered what powers a beautiful blog? It's a static site generator (SSG) called Eleventy, which is super simple to get started with.
But the main reason for me to use Eleventy (11ty) is that it's so blazing fast and customizable.
Yes I've tried some others like jekyll, but somehow 11ty was so much easier for me (opinion yes 🤫)
Let's dive into creating a simple blog with Eleventy! 🔥

Setting up the project

Like other articles like this one about setting up node.js, we start by creating a basic folder structure.
mkdir blog
cd blog
This will create a directory called blog and change our directory into it.
npm install
This will install a basic node application.
npm install --save-dev @11ty/eleventy
This will add Eleventy to our package, according to their docs a local installation is preferred over a global one.
That's it; We can now start working on adding some pages.

Adding a homepage to Eleventy

Of course, every website/blog needs a homepage, so let's start with opening the project in your favorite editor.
First, we will add a file in our root called index.njk
njk is short for Nunjucks which is a templating language
---
layout: layouts/home.njk
---

<h1>Latest posts will come here</h1>
As you can see, it's a mix between Markdown and Layout.
We define a layout for this file up top layouts/home.njk which we will create in a bit.
Now let's create out layout structure
Create a folder called 
_includes
 in that we create another folder layouts
First we create our base layout, create a file called base.njk
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
    <meta
      name="Description"
      content=""
    />
    <link rel="stylesheet" href="/css/index.css" />
  </head>
  <body>
    <main >
      
    </main>
  </body>
</html>
And our home.njk file
---
layout: layouts/base.njk
templateClass: tmpl-home
---

{{ content | safe }}
Running Eleventy local
To see what we create so far run the following command in your terminal
npx eleventy --serve
The open up http://localhost:8080 to see what we got!
Nothing fancy, but it should show an H1 with the text Latest posts will come here.

Adding blog functionality to Eleventy

A blog wouldn't be much without actual blog posts, so let's dive into creating some blog posts.
Start by creating a new folder in your root called posts we will then add 5 Markdown files.
---
title: Post number 1
description: Whatever you want to say
date: 2020-04-17
tags:
  - posts
layout: layouts/post.njk
---

# Lorem ipsum dolar si amet!?

Yes! I agree
Follow through these until you have 5, let's call them post-one.md.
The important part here is the tags option, we can have multiple tags, but in this case we set one being posts this will become our collection!
Also we mention a new layout here so lets create _includes/layouts/post.njk
---
layout: layouts/base.njk
templateClass: tmpl-post
---

<p><a href="{{ '/' | url }}">Back home</a></p>
<hr />
<h1>{{ title }}</h1>

{{ content | safe }}
Nothing fancy, we extend the base template again and add an essential back to home button.
Then we show the Title of this blog post and the content.
No, let's go back to our index.njk file and change it to the following:
---
layout: layouts/home.njk
---

<h1>Latest posts</h1>

{% set postslist = collections.posts %} {% include "postslist.njk" %}
Like mentioned in the post files we included a tag called posts and here we are getting those collections called posts!
You can also use collections.all to get ALL collections.
Now let's make the actual postlist.njk
<ul>
  {%- for post in postslist -%}
  <li><a href="{{ post.url }}">{{ post.url }}</a></li>
  {%- endfor -%}
</ul>
Here we loop through our postlist and make a url to that specific post.
Now run the following command again:
npx eleventy --serve
Yes!
We now have our working blog, Let's work from here and make it more awesome!
You can find this project on Github here.
See how we can host this blog on Netlify.
Previously published at https://daily-dev-tips.com/posts/building-a-static-blog-with-11ty/

Written by daily-dev-tips | https://daily-dev-tips.com I write daily dev tips to contribute to the development community!
Published by HackerNoon on 2020/04/21