Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Taxonomy based dynamic menu system

Started by Muut Archive 11 years ago · 1 replies · 408 views
11 years ago

I've tried a couple of things, and I'm not sure what I've got so far is the best way to do this. I have a series of 5 categories, which correspond to a set of 5 top-level directories. These 5 categories make up the top level navigation.

Within these 5 directories are all of the pages that should be listed in the sub-nav when one of the categories is selected. So the structure is:
--- html
├── pages
├── 01.category
├── 01.page
└── docs.md
├── 02.page
└── docs.md
└── category.md
├── 02.category
├── 01.page
└── docs.md
├── 02.page
└── docs.md
└── category.md

TXT
I'm using the sidebar code from the rtfm theme, and on the top-level it works great:

{% macro loop(page, parent_loop) %}
{% if parent_loop|length > 0 %}
{% set data_level = parent_loop %}
{% else %}
{% set data_level = 0 %}
{% endif %}

{% for p in page.children.visible %}
{% set parent_page = p.activeChild ? ' parent' : '' %}
{% set current_page = p.active ? ' active' : '' %}
<li class="dd-item{{ parent_page }},{{ current_page }}" data-nav-id="{{ p.route }}">
{% if p.children.count > 0 %}
<span class="group-name">{{ p.menu }}</span>
<ul>
{{ _self.loop(p, parent_loop+loop.index) }}
</ul>
{% else %}
<a href="{{ p.url }}"><span>{{ p.menu }}</span></a>
{% endif %}
</li>
{% endfor %}
{% endmacro %}

{% macro version(p) %}
{% set parent_page = p.activeChild ? ' parent' : '' %}
{% set current_page = p.active ? ' active' : '' %}
<h5 class="{{ parent_page }},{{ current_page }}">
{% if p.activeChild or p.active %}
<i class="fa fa-chevron-down fa-fw"></i>
{% else %}
<i class="fa fa-plus fa-fw"></i>
{% endif %}
<a href="{{ p.url }}">{{ p.menu }}</a>
</h5>
{% endmacro %}

{% if theme_config.top_level_version %}
{% for slug, ver in pages.children %}
{{ _self.version(ver) }}
<ul id="{{ slug }}" class="topics">
{{ _self.loop(ver, '') }}
</ul>
{% endfor %}
{% else %}
<ul class="topics">
{{ _self.loop(pages, '') }}
</ul>
{% endif %}

TXT


once you click on any of the pages the parent_loop gets refreshed, and the menu changes. I'd like it to be more or less static once it's built after selecting one of the categories. And right now, I'm drawing a blank.

Here's a screenshot as well. [Top-level-category](//muut.com/u/getgrav/s2/:getgrav:4TN3:toplevelcategory.jpg.jpg)
11 years ago

What is the benefit of using taxonomy based navigation? I think what you are wanting to do would be better served by just creating those category folders with your top-level categories and then subfolders representing sub-categories etc.

Basically recreate your folder structure as your category structure? This would work with the default menu code as-is.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1348 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago