ZYVOPMulti-Platform Sync
SeriesAI NewsWhy ZyVOPJoin Discord
LoginGet Started
ZYVOP
The Developer Publishing Hub
PrivacyTermsGuidelinesDMCACommunity
© 2026 ZyVOP
HomeLet's Learn 11ty Part 3: Collections, Shortcodes, Macros

Let's Learn 11ty Part 3: Collections, Shortcodes, Macros

Collections, Shortcodes, Macros

James 'Dante' Midzi
James 'Dante' Midzi
Solutions Architect
·
June 1, 2022
3 min read
Originally published ondantedecodes.vercel.app
Series

Eleventy

Part 4 of 4

Prev
Next
Let's Learn 11ty Part 3: Collections, Shortcodes, Macros
#tutorial#eleventy#SSG

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

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

---
layout: base
title: Blog
tags: page
---

# Blog Home
  • Now let's go into _navigation.njk and change it to this:

<nav>
  {%- for item in collections.page -%}
  <a href="{{ item.url }}">{{ item.data.title }}</a>
  {%- endfor -%}
</nav>

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:

---
title: First Post
layout: base
tags: post
---

# First Post
  • Now modify blog.njk to look like this:

---
layout: base
title: Blog
tags: page
---

<h1>Blog Home</h1>

<ul>
  {% for post in collections.post %}
  <li>
    <a href="{{ post.url }}">{{ post.data.title }}</a>
  </li>
  {% endfor %}
</ul>

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:

/* 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) =>
      `<h1>${title}</h1>
        <p>${subtitle}</p>`,
  );

  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:

---
layout: base
title: Home
tags: page
---

<img src="/assets/images/learn.png" alt="" />

<!-- Shortcode in use -->
{% headers "Home", "Learning SSG"%}

<br />

I am a site built with <a href="https://www.11ty.io/">Eleventy</a>

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

<!-- _includes/macros/headers.njlk -->
{% macro pageHeading(heading, subheading) %}

<h1>{{heading}}</h1>
<p>{{subheading}}</p>

{% endmacro %}

And it would be used like this:

{% 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

Series

Eleventy

Part 4 of 4

Prev
Next

Comments (0)

Join the discussion by logging into your account.

James 'Dante' Midzi
James 'Dante' Midzi

Solutions Architect

Knowledge and the sharing of it drives me. Along with improvement each day

Subscribe to James 'Dante' Midzi 's Newsletter

Direct email dispatches when new stories are published. Zero algorithms.

Like
Love
Clap
Fire
Party
Wow

More from James 'Dante' Midzi

View profile

The Reason I Prefer Opinionated UI Libraries

Explore how opinionated UI libraries like Nuxt UI simplify form handling and reduce boilerplate, compared to headless kits like ui‑thing.

3 minSep 3

Why I Avoid shadcn/ui: Design Homogenisation & Hype

Explore why shadcn/ui may lead to generic, Bootstrap‑like sites and how LLMs amplify the trend, plus my reasons for skipping this popular UI kit.

2 minSep 1

The 'AI' Problem Noone Is Talking About

In a world where the physical books are gone and one company controls your access to those digital copies, nothing stops them from denying you access.

4 minAug 25

Are You Crippled By Your Preferences

In my head as a developer, you're supposed to be able to learn anything you're driven to. Your driving tenet should be to learn. If you don't understand something you take the time to learn what it wants not what you want.

3 minAug 17

Strict Types Won't Fix Your Lack of Understanding

In development circle I am what would be considered a mid/senior developer. I have been in the trenches, seen the rise and fall of technologies. Seen the prophe...

4 minAug 7