nav
Display site navigations.
{{ nav }}
Displays a list of folders and pages. Recently updated in v1.7
to traverse deeper through recursion.
Simple Example
<ul class="nav">
{{ nav from="/" max_depth="2" }}
<li>
<a href="{{ url }}" {{ if is_current }} class="current"{{ endif }}>
{{ title }}
</a>
</li>
{{ /nav }}
</ul>
Parameters
from
The folder from which to start building your nav.
Default: the current folder based on your URL.
{{ nav from="/" }}
exclude
A pipe-separated list of folders to exclude
{{ nav from="/" exclude="site-map|chamber-of-secrets" }}
max_depth
Maximum number of subdirectory levels to traverse, letting you build a multi-level nav.
Default: 1
{{ nav from="/" max_depth="2" }}
include_entries
You can include entries in your nav by setting to true
. Somewhat of an edge case, but it makes the Nav tag capable of fetching just about any and all of your content.
Default: false
{{ nav from="case-studies" include_entries="true" }}
folders_only
Only include page.md
files when fetching content. This is the default behavior as it generally lets you build a nav of your content sections and respective parent/landing pages.
Default: true
{{ nav from="/" folders_only="false" }}
include_content
Whether to include all content fields when fetching pages/entries.
Default: true
(Since 1.10)
{{ nav from="/" include_content="false" }}
limit
If set, limits the the maximum number of pages to display.
{{ nav from="/" limit="4" }}
offset
If set, skips over the specified number of pages.
{{ nav from="/" offset="4" }}
Tag Variables
The following variables are available within the nav tag:
is_current
— a boolean as to whether the item in the current iteration of the loop matches the URL being viewedis_parent
— a boolean as to whether the item in the current iteration of the loop is a parent of the URL being viewedurl
— the URL to this foldertitle
— the name of this folder, as pulled from thepage
file within itchildren
— if there are subdirectories found, a list of subdirectories within this folderparent
(added in v1.7.5) — a tag pair that gives you access to information about the parent page for the current child content being looped through, within this tag pair you can always use{{ url }}
and{{ title }}
, and if yournav
tag setinclude_content
totrue
, you can also use any other variable for that content
Recursion (added in 1.7)
It’s also now possible to use recursive tags for semi-automatically creating deeply-nested lists of links as your navigations. A sample set-up looks something like this:
<ul>
{{ nav from="/{segment_1}" max_depth="4" folders_only="false" }}
<li>
<a href="{{ url }}"{{ if is_current || is_parent }} class="on"{{ endif }}>{{ title|smartypants|widont }}</a>
{{ if is_current || is_parent }}
{{ if children }}
<ul>{{ *recursive children* }}</ul>
{{ endif }}
{{ endif }}
</li>
{{ /nav }}
</ul>
This example is from the Acadia theme’s sub-navigation partial. The {{ *recursive children* }}
tag will repeat the contents of the entire {{ nav }}
tag using child elements if they exist. This means that as long as there are children to display, and we’re still on either the current or parent page of those children, the nav tag will traverse deeper.
It’s an admittedly weird concept, and might take some fiddling with to truly understand, but is very powerful when fully understood. Take the time. Learn to wield it. A powerful jedi will you be.