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

Sidebar menu that doesn't change after page selection

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

This has been plaguing me for a while, and while I'm sure it's something simple, I'm stuck. I'm trying to create a sidebar menu that lists only what is inside a specific folder. This code:
---html
<ul class="topics">
{% for p in taxonomy.findTaxonomy({'category':page.taxonomy.category}) if p != page %}
<li class="dd-item" data-nav-id="{{ p.route }}">
{% if p.children|length > 0 %}
<p>{{ p.title }}</p>
<ul class="sub-topics">
{% for cp in p.children %}
<li><a href="{{cp.url}}">{{ cp.title }}</a></li>
{% endfor %}
</ul>
{% else %}
{% if p.parent.title == page.taxonomy.category[1] %}
<a href="{{ p.url|escape }}" title="{{ p.title }}">{{ p.title }}</a>
{% endif %}
{% endif %}
</li>
{% endfor %}
</ul>

TXT
Mostly does the trick. However, if I click on one of the links it creates, the menu changes and the menu rebuilds listing all of the folders at the top of the structure. So if my menu starts like this:
--- 
Layout (folder)
   Header (page)
   Grid
   Lists
   Elapsed Time
   Timestamp
   Avatar
   HR
Lists
   Ordered Lists
   Unordered Lists
Navigation
   Primary Navigation
   Breadcrumbs
   Pagination
   Tabs
User Input
   Typeahead
   Select Menu
   Text Fields
   Checkbox
   Radio Button
   Search
Actions
   Link States
   Default Buttons

It ends up with this at the top:

TXT

Patterns
   Layout
   Navigation
   Data-display
   Data-controls
   Formatting
   User Input
   Actions
Layout
   Header
   Grid
   Lists
   Elapsed Time
   Timestamp
   Avatar
   HR
Lists
   Ordered Lists
   Unordered Lists
---
11 years ago

There's probably a better way to do this, but this is what I have that creates the menu how I want it based on a series of directories.

TWIG
{% macro loop(page) %}
  {% for p in page.children %}
    {% if p.visible %}
      {% set current_page = (p.active or p.activeChild) ? 'active' : '' %}

      {% if p.children.count > 0 %}
        <li>
          <p>{{ p.title }}</p>
          <ul class="sub-topics">
            {{ _self.loop(p) }}
          </ul>
        </li>
      {% else %}
        <li class="dd-item" data-nav-id="{{ p.route }}">
          <a href="{{ p.url }}" title="{{ p.title }}">{{ p.title }}</a><i class="{{ page.status }}"></i>
        </li>
      {% endif %}
    {% endif %}
  {% endfor %} 
{% endmacro %}

<ul class="topics">
  {% for p in taxonomy.findTaxonomy({'category':page.taxonomy.category}) %}
    {% if p.title|lower != 'patterns' and p.title|lower != 'styling' and p.title|lower != 'anatomy' %}
      {% if p.children.count > 0 %}
        <li>
          <p>{{ p.title }}</p>
          <ul class="sub-topics">
            {{ _self.loop(p) }}
          </ul>
        </li>
      {% else %}
        {{ _self.loop(p) }}
      {% endif %}
    {% endif %}
  {% endfor %}
</ul>

The menu was changing as I navigated into the directory and the route changed.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1323 9 years ago
Archive · by Muut Archive, 9 years ago
2 920 9 years ago
Archive · by Muut Archive, 9 years ago
2 4048 9 years ago
Archive · by Muut Archive, 9 years ago
1 2924 9 years ago
Archive · by Muut Archive, 9 years ago
3 1106 9 years ago