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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Show a menu if a condition is true

Solved by Makary View solution

Started by mikael 9 years ago · 7 replies · 1700 views
9 years ago

Hey!

I am making a site that includes several modular pages. The one page menu works great for each modular page, but I wish to show the main menu on all sub-modular pages, I know it can be done, but tried so many different things that I have to ask.
I have tried and some more:

TWIG
{% if page.header.level1menu %}

{% if page.header.level1menu == true %} 

{% if find.page("/en/1st-level/health") == true %}

Let me explain what in need in the code:

=== Start of problem

TWIG
{% if in a sub-modular page then show %}

=== End of problem

TWIG
<div class="row">
<nav class="nav">
{% if theme_config.dropdown.enabled %}
    {{ _self.loop(pages) }}
{% else %}
    {% for page in pages.children.visible %}
        {% set current_page = (page.active or page.activeChild) ? 'selected' : '' %}
        <li class="nav-item {{ current_page }}">
            <a href="{{ page.url }}" class="nav-link active">
                {% if page.header.icon %}<i class="fa fa-{{ page.header.icon }}"></i>{% endif %}
                {{ page.menu }}
            </a>
        </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 %}
</nav>
</div>

=== Ending problem if

TWIG
 {% endif %}

=== Ending problem if

TWIG
<div class="row">
{% if show_onpage_menu %}
    <nav id="sticky-top" class="nav w-100 green1bg">
    {% for module in page.collection() %}
        {% set current_module = (module.active or module.activeChild) ? 'active' : '' %}
        <li class="nav-item {{ current_module }}"><a href="#{{ _self.pageLinkName(module.menu) }}" class="nav-link">{{ module.menu }}</a></li>
    {% endfor %}
    {% for mitem in site.menu %}
        <li>
            <a {% if mitem.class %}class="{{ mitem.class }}"{% endif %} href="{{ mitem.url }}">
                {% if mitem.icon %}<i class="fa fa-{{ mitem.icon }}"></i>{% endif %}
                {{ mitem.text }}
            </a>
        </li>
    {% endfor %}
  </nav>
{% else %}
    {{  parent() }}
{% endif %}
  </div>
{% endblock %}
{% block content %}
{{ page.content }}
{% for module in page.collection() %}
<div id="{{ _self.pageLinkName(module.menu) }}"></div>
    {{ module.content }}
{% endfor %}
{% endblock %}

My goal is to show the main menu if in a sub-modular page.

Thank you 🙂

last edited 07/30/17 by mikael
9 years ago

Hi there!
If you wish to display main menu as in menu containing all the top level pages the code that should work will be

TWIG

{% for page in pages.children %}
//Display a link:
<a href="{{ page.url }}" >{{ page.menu }}</a>
{% endfor %}
9 years ago

That i know as it is already the part of the code. The thing i want to do is only include that menu if in a sub-modular page. Let me clarify the part in the code.

9 years ago

So, you wish to display it on each and every module? If so, perhaps just add this code to all the module templates...
Or do you wish to show it if page contains modules? If this is the case, use
{% if page.children.count() > 0 %} condition :)

👍 1
9 years ago

Thank you it works 🙂

The problem is that it is also showing on the home. One the homepage i only need the one page menu.
This would be optimal:

TWIG
{% if page.children.count() > 0 and base_url == false %}   // and the page is not home.

I am concidering using a custom menu using:

TWIG
{% 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 %}

Since i have to organize the content is such a way that it will be easy to use even when filled with a lot of content.

So I think I need an if statement that show a custom menu everywhere but the frontpage. The frontpage has links to the different parts of the site and so I only need a top menu to navigate back to the main topics on the frontpage.

I will soon be able to show the site and how it works, then it will be clear what is needed.

Thank you very much for helping me! I try my best to find the answeres, but grav is unfortunatly not big enough to have a search result for everything. But that will soon change!

9 years ago Solution

{% if not page.home() and page.children.count() > 0 %} should do the trick.

👍 1
9 years ago

Thank you!!! Thank you!!! Thank you!!! Hahah so cool! .😃

👍 1
9 years ago

I'm glad it worked! Enjoy your adventure with Grav!

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 197 2 months ago
Themes & Styling · by Ian, 2 months ago
3 92 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 454 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 47 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 127 3 months ago