diff --git a/docs/_tutorials/navigation.md b/docs/_tutorials/navigation.md index 87368b0f..bcb9fe15 100644 --- a/docs/_tutorials/navigation.md +++ b/docs/_tutorials/navigation.md @@ -605,3 +605,65 @@ The `for item in items` loop looks through each `item` and gets the `title` and For more details on the `group_by` filter, see [Jekyll's Templates documentation](https://jekyllrb.com/docs/templates/) as well as [this Siteleaf tutorial](https://www.siteleaf.com/blog/advanced-liquid-group-by/). For more details on the `sort` filter, see [sort](https://shopify.github.io/liquid/filters/sort/) in Liquid's documentation. Whether you use properties in your doc's front matter to retrieve your pages or a YAML data file, in both cases you can programmatically build a more robust navigation for your site. + +## Scenario 9: Nested tree navigation with recursion + +Suppose you want a nested tree navigation of any depth. We can achieve this by recursively looping through our tree of navigation links. + +**YAML** + +```yaml +nav: + - title: Deployment + url: deployment.html + subnav: + - title: Heroku + url: heroku.html + subnav: + - title: Jekyll on Heroku + url: jekyll-on-heroku.html + - title: Help + url: help.html +``` + +**Liquid** + +First, we'll create an include that we can use for rendering the navigation tree. This file would be `_includes/nav.html` + +{% raw %} +```liquid + +``` +{% endraw %} + +To render this in your layout or pages, you would simply include the template and pass in the `nav` parameter. In this case, we'll use the `page.nav` to grab it from the yaml frontmatter. + +{% raw %} +```liquid +{% include nav.html nav=page.nav %} +``` +{% endraw %} + +Our include will use this first, then look through each item for a `subnav` property to recursively render the nested lists. + +**Result** +
+ +