Show a menu if a condition is true

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:

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

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

=== End of problem

<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

 {% endif %}

=== Ending problem if

<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 :slight_smile:

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

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

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.

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 :slight_smile:

1 Like

Thank you it works :slight_smile:

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:

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

I am concidering using a custom menu using:

{% 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!

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

1 Like

Thank you!!! Thank you!!! Thank you!!! Hahah so cool! .:smiley:

1 Like

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