--- title: "Let's Learn 11ty Part 3: Collections, Shortcodes, Macros" author: "James 'Dante' Midzi " published: "2022-06-01T00:00:00.000Z" updated: "2026-09-25T14:41:19.442Z" url: "https://zyvop.com/let-s-learn-11ty-part-3-collections-shortcodes-macros-xnk1g" canonical: "https://dantedecodes.vercel.app/articles/eleventy/lets-learn-11ty-part-3-collections-shortcodes-macros-p0a" tags: ["tutorial","eleventy","SSG"] --- # Let's Learn 11ty Part 3: Collections, Shortcodes, Macros Collections, Shortcodes, Macros ![Cover image]() So far we have look at the basic aspects of Eleventy. Let's now delve deeper into more granular details of Eleventy ## Collections In my opinion, **collections** are the cornerstone of Eleventy. Collections allow you group pieces of content is varying ways. This is done by using **tags**. ### Tags in Eleventy Tags in Eleventy work differently from tags you might have seen elsewhere. They have one purpose - to **create collections**. Whereas in other places you may have seen them, they are used as a hierarchy of labels for the content. if you recall, I mentioned that we would improve our navigation. We are going to do that by making use of collections. We will start by adding tags to our pages' YAML data ```md --- layout: base title: Home tags: page --- ``` Do the same for the `about.md` file. in addition, we will make a `blog.md` file in the same level as our other files - the `src` folder. ```md --- layout: base title: Blog tags: page --- # Blog Home ``` - Now let's go into `_navigation.njk` and change it to this: ```xml ``` In the code above: - From the collections we have made (In our pages), we loop over the collection - Then we display data from the collections in an anchor tag: - `item.url` refers to the URL relating to the page - `item.date.title` accesses the title we set in the YAML ![]() * * * ### Listing Blog Posts on Blog Home Let's use the same concept, make some blog posts and list them on the **Blog Home Page** - We'll start by changing `blog.md` to `blog.njk` - this will be necessary for what we want to do. - Then create a blog folder in `src` . In it, create a few posts in it like this: ```md --- title: First Post layout: base tags: post --- # First Post ``` - Now modify `blog.njk` to look like this: ```xml --- layout: base title: Blog tags: page ---

Blog Home

``` ![]() Pretty cool, huh? This is just a small look into collections, there is so much more you can do with the. And various ways you can make use of them: [Collections Docs]() * * * ## Shortcodes Shortcodes are a way to make use of reusable blocks of code. Say for example, on your site there is a structure that repeats on your site you would use a shortcode to simplify the adding of that content. > In simplest terms, a shortcode is a function Let's have a look and make our first shortcode. Say we wanted a title and subtitle on our main pages, we could make a shortcode, like this in `eleventy.config.js`: ```javascript /* eleventy.config.js */ export default function (eleventyConfig) { eleventyConfig.addPassthroughCopy("src/assets/css/style.css"); eleventyConfig.addPassthroughCopy("src/assets/images"); // ADD THIS eleventyConfig.addShortcode( "headers", (title, subtitle) => `

${title}

${subtitle}

`, ); return { dir: { input: "src", data: "_data", includes: "_includes", layouts: "_layouts", }, }; } ``` Now, we will change `index.md` to `index.njk` and change it's contents to this: ```xml --- layout: base title: Home tags: page --- {% headers "Home", "Learning SSG"%}
I am a site built with Eleventy This site is my effort to understand Eleventy. I am doing so by guiding others. Together, we will Learn Eleventy. ``` ![]() ## Notes - There is an addition/extension of shortcodes called a PairedShortcodes, where you can do more than just what we've done here. [Shortcode Docs]() - I have an implementation of them on my site here: [James Midzi]() - Whenever you modify a shortcode, you need to restart your development server for the changes to take effect * * * ## Macros Macros can be used in the same we use shortcodes, what differs is their implementaion. Take for example the shortcode we just made, the macro for it would look like this ```xml {% macro pageHeading(heading, subheading) %}

{{heading}}

{{subheading}}

{% endmacro %} ``` And it would be used like this: ```xml {% import "macros/headers.njk" as macro %} {{ macro.pageHeading("Home", "Learn Eleventy")}} ``` > ### My Advice > > Stick to shortcodes. Macros have a tendency of becoming complicated. > If you really have to, use the **common tags** package insted * * * ## Conclusion Our site is coming along nicely. - We improved our navigation - We looked at a way to make use of reusable data - We added a blog section to our site **As always**: - The working repo: [Part 3: Collections and Shortcodes]() - The docs: [https://www.11ty.dev/docs/]() - The discord: [https://www.11ty.dev/blog/discord/]() I'll catch you all in the next one :D * * * ### Thank you for reading, let's connect! Thank you for visiting this little corner of mine. My email and DMs are always open; if you want to chat or collaborate on something, [shoot me a email]() Where you can get hold of me: - [Twitter]() - Discord - [LinkedIn]()