Book Image

Eleventy by Example

By : Bryan Robinson
Book Image

Eleventy by Example

By: Bryan Robinson

Overview of this book

11ty is the dark horse of the Jamstack world, offering unparalleled flexibility and performance that gives it an edge against other static site generators such as Jekyll and Hugo. With it, developers can leverage the complete Node ecosystem and create blazing-fast, static-first websites that can be deployed from a content delivery network or a simple server. This book will teach you how to set up, customize, and make the most of 11ty in no time. Eleventy by Example helps you uncover everything you need to create your first 11ty website before diving into making more complex sites and extending 11ty’s base functionality with custom short codes, plugins, and content types. Over the course of 5 interactive projects, you’ll learn how to build basic websites, blogs, media sites, and static sites that will respond to user input without the need for a server. With these, you’ll learn basic 11ty skills such as templates, collections, and data use, along with advanced skills such as plugin creation, image manipulation, working with a headless CMS, and the use of the powerful 11ty Serverless plugin. By the end of this book, you’ll be well-equipped to leverage the capabilities of 11ty by implementing best practices and reusable techniques that can be applied across multiple projects, reducing the website launch time.
Table of Contents (13 chapters)

Writing a data file to automatically create an image array for each post

So, a post with a singular photo isn’t a big deal in Markdown. Toss the image URL inside Markdown’s image syntax – or better still, use the Image plugin we’re installing in the next section – and you’re done. If you have an entire gallery of photos for a post, things get bogged down and require a lot of manual work.

For our photo blog, let’s create a way to get an array of all the photos available for a specific blog post.

The basic structure of this setup is to have a series of image files in the same directory as the post’s Markdown file. Then, we’ll use the posts directory data file to create an array from all the files. The directory data file is run for each of the documents in the directory, meaning that if we use the eleventyComputed functionality, we can compute a unique array for each post.

To start, add a few images to each of your...