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

Sub-dropdown menus?

Started by Muut Archive 11 years ago · 15 replies · 1730 views
11 years ago

I'm trying to port the TXT theme from HMLT5 UP to Grav. However TXT has sub-dropdown menus, something that I find very useful. In the http://saveourplanet.org/ (which uses the TXT theme as a base), I hardcoded the sub-dropdown menus in. Later on I got it a little more dynamic, but it's still not right. I currently have it hardcoded in to only show sub-dropdowns for /about/team, since it was showing blank ones for all menu items. How would I go about properly implementing this? Here's my current navigation.html.twig for the above site:

TWIG

{% macro loop(page) %}
    {% for p in page.children.visible %}
        {% set current_page = (p.active or p.activeChild) ? 'active' : '' %}
        <li class="{{ current_page }}">
            <a href="{{ p.url }}">
                {% if p.header.icon %}<i class="fa fa-{{ p.header.icon }}"></i>{% endif %}
                {{ p.menu }}
            </a>
            {% if p.children.visible.count > 0 %}
                <ul>
                    {{ _self.loop(p) }}
                </ul>
            {% endif %}
        </li>
    {% endfor %}
 {% endmacro %}
<nav id="nav">
    <ul>
        {% if theme_config.dropdown.enabled %}
            {{ _self.loop(pages) }}
        {% else %}
            {% for page in pages.children.visible %}
                {% set current_page = (page.active or page.activeChild) ? 'active' : '' %}
                <li class="{{ current_page }}">
                    <a href="{{ page.url }}">
                        {% if page.header.icon %}<i class="fa fa-{{ page.header.icon }}"></i>{% endif %}
                        {{ page.menu }}
                    </a>
                    {% if page.header.dropdown %}
                        <ul>
                            {% for page in page.children.visible %}
                                <li>
                                  <li class="{{ current_page }}">
                                      <a href="{{ page.url }}">
                                          {% if page.header.icon %}<i class="fa fa-{{ page.header.icon }}"></i>{% endif %}
                                          {{ page.menu }}
                                      </a>
                                      {% if page == page.find('/about/team') %}
                                          <ul>
                                              {% for page in page.children.visible %}
                                                  <li>
                                                    <li class="{{ current_page }}">
                                                        <a href="{{ page.url }}">
                                                            {% if page.header.icon %}<i class="fa fa-{{ page.header.icon }}"></i>{% endif %}
                                                            {{ page.menu }}
                                                        </a>
                                                    </li>
                                                  </li>
                                              {% endfor %}
                                          </ul>
                                      {% endif %}
                                  </li>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        {% endif %}
        {% for mitem in site.menu %}
            <li>
                <a href="{{ mitem.url }}">
                    {% if mitem.icon %}<i class="fa fa-{{ mitem.icon }}"></i>{% endif %}
                    {{ mitem.text }}
                </a>
            </li>
        {% endfor %}
    </ul>
</nav>
---
11 years ago

I'm a bit confused with your code. The macro should recurse through all your pages and display the menu as a nested list. However, you seem to have manually hardcoded the same sort of thing below?

Whey don't you a) set the dropdown: enabled: true flag in your theme, so it will use the top bit. or b) remove the check and force it to use the macro.

You may need to adjust the html output in the macro to match your required output.

11 years ago

OK, so I tried using the antimatter navigation code instead, and put dropdown: enabled: true in the site.config, blueprints.yaml, and txt.yaml, but there aren't any dropdowns. What am I missing?

11 years ago

I was giving the shorthand syntax. Please don't randomly put this in various places. There is only one place to put it: user/config/themes/yourtheme.yaml whatever your theme is called. Syntax is as outlined in the antimatter README.md: https://github.com/getgrav/grav-theme-antimatter:

YAML
dropdown:
  enabled: true
11 years ago

Sorry, I noticed about theme config after I posted. OK. I tried that, and it didn't fix the problem. What's my next course of action? Are there any other files that make the dropdown work besides navigation.html.twig and the theme config file?

11 years ago

In Antimatter, having

YAML
dropdown:
  enabled: true

in user/config/themes/antimatter.yaml should just work, tested now. Can you first try with Antimatter directly and then with your theme implementation?

11 years ago

OK, It shows drop and sub-dropdowns fine in the mobile version of Antimatter, but the desktop version isn't showing a menu at all with my site.

11 years ago

I'm uploading it to my server right now. One sec.

11 years ago

You know, I based my custom theme on Sidewalk. Maybe I should re-do it using Antimatter as a base?

11 years ago

If you view the page you will see that there's nothing output in the top part of the page where teh navigation should be:

HTML
<div id="navbar">
                                                            <ul class="navigation">
            </ul>
                    <span class="panel-activation sb-toggle-left navbar-left menu-btn fa fa-bars"></span>
            </div>

However it is displayed just fine in the bottom sb-slidebar div.

So the problem is your simply not including the navigation at the top as you are at the bottom.

11 years ago

Isn't the menu it supposed to be automatically generated with Antimatter? Is that something that custom page headers could be causing?

11 years ago

Well it is, but you clearly have done something to it. If you just install the antimatter theme from GPM and change your theme setting in system.yaml to use it, i'm sure the menu will show up fine.

11 years ago

Oddly enough, that's what I did for that test I showed you.

11 years ago

I'm thinking the best thing to do is start over with Antimatter and rebuild the site. I obviously really messed something up. Thanks for the help!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1344 9 years ago
Archive · by Muut Archive, 9 years ago
2 930 9 years ago
Archive · by Muut Archive, 9 years ago
2 4058 9 years ago
Archive · by Muut Archive, 9 years ago
1 2943 9 years ago
Archive · by Muut Archive, 9 years ago
3 1116 9 years ago