Skip to content

Instantly share code, notes, and snippets.

@hatarist
Created September 27, 2016 20:14
Show Gist options
  • Save hatarist/a122d201e37d8586072f7d05635bfe19 to your computer and use it in GitHub Desktop.
Save hatarist/a122d201e37d8586072f7d05635bfe19 to your computer and use it in GitHub Desktop.
{# include this template right at the beginning of the <body> tag in your base.html #}
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
{# +- title, login_required, urls #}
{# +-- url, login_required, title #}
{% set elements = [
("Index", True, [
(url_for('webui.index'), True, "Index")
]),
("Stuff", True, [
(url_for('webui.stuff'), True, "Stuff")
]),
("Multilevel", True, [
(url_for('webui.multilevel1'), True, "Multilevel 1"),
(url_for('webui.multilevel2'), True, "Multilevel 2"),
('---'),
(url_for('webui.multilevel3'), True, "Multilevel 3"),
(url_for('webui.multilevel4'), True, "Multilevel 4"),
]),
("More", True, [
(url_for('webui.more1'), True, "More 1"),
('---'),
(url_for('webui.more2'), True, "More 2"),
(url_for('webui.more3'), True, "More 3"),
]),
("Kakawka", True, [
(url_for('webui.kakawka1'), True, "Kakawka1"),
(url_for('webui.kakawka2'), True, "Kakawka2"),
(url_for('webui.kakawka3'), True, "Kakawka3"),
]),
] %}
{% for name, login_required, urls in elements %}
{% if urls|length > 1 %}
{% if not login_required or (login_required and current_user.is_authenticated) %}
<li class="dropdown {% for url, _, subname in urls %}{% if url in request.path %}active{% endif %}{% endfor %}">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
{{ name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
{% for url, sub_login_required, sub_name in urls %}
{% if url == '-' %}
<li role="separator" class="divider"></li>
{% else %}
{% if not sub_login_required or (sub_login_required and current_user.is_authenticated) %}
<li{% if url in request.path %} class="active"{% endif %}>
<a href="{{ url }}">{{ sub_name }}</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
</li>
{% endif %}
{% else %}
{% if not login_required or (login_required and current_user.is_authenticated) %}
<li{% if urls[0][0] in request.path %} class="active"{% endif %}>
<a href="{{ urls[0][0] }}">{{ name }}</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
{% if current_user.is_authenticated %}
<li>
<form class="navbar-form" role="search" action="{{ url_for('webui.search') }}" method="get">
<div class="input-group add-on">
<input type="text" class="form-control" placeholder="Search" name="q" id="q">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</li>
{% endif %}
</ul>
<ul class="nav navbar-nav navbar-right">
{% if current_user.is_authenticated %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-user" aria-hidden="true"></span>
{{ current_user.username }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">Profile</a></li>
<li><a href="{{ url_for('webui.settings') }}">Settings</a></li>
<li role="separator" class="divider"></li>
<li><a href="{{ url_for('webui.logout') }}">Logout</a></li>
</ul>
</li>
{% else %}
<li><a href="{{ url_for('webui.login') }}">Log in</a></li>
<li><a href="{{ url_for('webui.register') }}">Register</a></li>
{% endif %}
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment