Add the eleventyNavigation object to your front matter data (or in a data directory file). Assign a unique string to the key property inside of eleventyNavigation.
Example
mammals.md
---eleventyNavigation:key: Mammals
---
This gives us:
Mammals
humans.md
To nest a template inside of the Mammals template, use parent: Mammals:
You can nest these as deep as you want! Want to put something under Humans or Bats? Use parent: Humans or parent: Bats. If you want to add another root template, leave out parent.
Use alternate text for the navigation link
If you want your key and your link text to be different, use the title property:
---eleventyNavigation:key: Mammals
title: All of the Mammals
---
Re-Ordering Items
To ensure that Humans comes first before Bats, use the order property. It can have an arbitrary number. If omitted, order: 0 is the default.
Added in Navigation v0.1.4 If you’d like to add a link to an external URL that is not on your local page, create a new file for it and add a url key.
---eleventyNavigation:key: Zach’s site
url: https://www.zachleat.com/
permalink:false---
Use permalink: false to ensure that this meta-template doesn’t create a file in your Eleventy site output.
Rendering the Navigation Menu (Easy Mode)
Template languages that support universal filters are supported. If you’re tired of reading, just use one of the following. These are using the filters documented below. If you want more control or need additional customization, keep reading!
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
To Markdown
Added in Navigation 0.3.1 This is most useful in .md files (preprocessed as Liquid or Nunjucks). It’s highly unlikely you want to output Markdown in a WebC file (but maybe you do, I’m not your parent). You probably want the HTML example above.
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
Advanced: Rendering the Navigation Bar (Deep Dive)
Fetch the menu items using the eleventyNavigation Filter
The eleventyNavigation filter returns a sorted array of objects with url and title properties (sorted using order, as noted above). If an entry has nested children, it will also include a children property with an array of similar objects (and those may contain children too, and so on).
Example: Fetch all pages
For our documented templates above with the following template:
{%setnavPages=collections.all|eleventyNavigationBreadcrumb("Does not exist",{allowMissing:true})%}{{navPages|dump|safe}}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
Render the menu items using the eleventyNavigationToHtml or eleventyNavigationToMarkdown Filters
There are a couple of methods for rendering. Using the eleventyNavigationToHtml and eleventyNavigationToMarkdown filters will render the full navigation tree. Use this if you want to easily scale to an unlimited number of tiers/levels in your navigation. If you want full control of the markup, render the structure manually using the Copy and Paste templates example below. Use this if your navigation will have one level/tier of items.
With the Navigation structure returned from eleventyNavigation or eleventyNavigationBreadcrumb, we can render the navigation. Pass the object to the eleventyNavigationToHtml or eleventyNavigationToMarkdown filter to automatically output the full menu (as HTML or Markdown):
The eleventyNavigationToMarkdown filter is Added in Navigation 0.3.1.
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
Showing excerpts
You can also use this to display a longer list of navigation items with description text. This is useful for category/index pages. Add excerpt to the eleventyNavigation object.
---eleventyNavigation:key: Mammals
excerpt: Vertebrate animals of the class Mammalia.
---
When you render a navigation list, pass showExcerpt: true to the eleventyNavigationToHtml filter, like so:
---js
{
navigationOptions: {
listElement: "ul", // Change the top level tag
listItemElement: "li", // Change the item tag
listClass: "", // Add a class to the top level
listItemClass: "", // Add a class to every item
listItemHasChildrenClass: "", // Add a class if the item has children
activeListItemClass: "", // Add a class to the current page’s item
anchorClass: "", // Add a class to the anchor
activeAnchorClass: "", // Add a class to the current page’s anchor
useAriaCurrentAttr: true, // Add aria-current="page" to the current page’s anchor
// If matched, `activeListItemClass`, `activeAnchorClass`, and `aria-current` will be added
activeKey: "",
// It’s likely you want to pass in `eleventyNavigation.key` here, e.g.:
// activeKey: eleventyNavigation.key
// Show excerpts (if they exist in data, read more above)
showExcerpt: false
}
}
---
{{ collections.all |eleventyNavigation|eleventyNavigationToHtml: navigationOptions |json}}
Syntax Nunjucks
---js
{
navigationOptions: {
listElement: "ul", // Change the top level tag
listItemElement: "li", // Change the item tag
listClass: "", // Add a class to the top level
listItemClass: "", // Add a class to every item
listItemHasChildrenClass: "", // Add a class if the item has children
activeListItemClass: "", // Add a class to the current page’s item
anchorClass: "", // Add a class to the anchor
activeAnchorClass: "", // Add a class to the current page’s anchor
useAriaCurrentAttr: true, // Add aria-current="page" to the current page’s anchor
// If matched, `activeListItemClass`, `activeAnchorClass`, and `aria-current` will be added
activeKey: "",
// It’s likely you want to pass in `eleventyNavigation.key` here, e.g.:
// activeKey: eleventyNavigation.key
// Show excerpts (if they exist in data, read more above)
showExcerpt: false
}
}
---
{{collections.all|eleventyNavigation|eleventyNavigationToHtml(navigationOptions)|safe}}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page
These work with eleventyNavigationBreadcrumb | eleventyNavigationToHtml too.
If you find yourself using a lot of these class options, maybe you should use the Advanced: Unlimited Child Levels example below and have full control of your HTML!
Bring your own HTML: Render the menu items manually
This template will render a single tier of items (no children) without using the eleventyNavigationToHtml or eleventyNavigationToMarkdown filters. This method gives you full control of the markup but is more complex with deeply nested menu structures.
Note that eleventyNavigationToMarkdown is Added in Navigation 0.3.1.