Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breadcrumb functionality #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ coverage.xml
docs/_build/

# PyBuilder
target/
target/

# VSCode
.vscode/
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Contact us at `[email protected] <mailto:[email protected]>`_
* Sami Hiltunen <[email protected]>
* Sylvain Boily <[email protected]>
* Marlin Forbes <[email protected]>
* Grzegorz Gyczew <[email protected]>
77 changes: 77 additions & 0 deletions examples/menu_and_breadcrumbs/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from flask import Flask, render_template
import flask_menu as menu

app = Flask(__name__, template_folder='')
menu.Menu(app=app)

@app.route('/')
@menu.register_menu(app, '.', 'Home')
def index():
return render_template('index.html')

@app.route('/first')
@menu.register_menu(app, '.first', 'First', order=0)
def first():
return render_template('index.html')

@app.route('/second')
@menu.register_menu(app, '.second', 'Second', order=1)
def second():
ids = [1, 2, 3, 4, 5]
details_route = 'second_details'
return render_template('index.html',
context={'ids': ids, 'details_route': details_route}
)

@app.route('/second/<int:id>')
@menu.register_menu(app, '.second.details', 'Details', id=id)
def second_details(id):
headline = f'Details of Second id: {id}'
details = f'Here are more details for Second id: {id}'
return render_template('index.html',
context={'headline': headline, 'details': details}
)

@app.route('/third')
@menu.register_menu(app, '.third', 'Third', order=3, dropdown=True)
def third():
ids = [1, 2, 3]
details_route = 'third_details'
return render_template('index.html',
context={'ids': ids, 'details_route': details_route}
)

@app.route('/third/<int:id>')
@menu.register_menu(app, '.third.details', 'Details', id=id, hide_on_menu=True)
def third_details(id):
headline = f'Details of Third id: {id}'
details = f'Here are more details for Third id: {id}'
return render_template('index.html',
context={'headline': headline, 'details': details}
)

@app.route('/third/a')
@menu.register_menu(app, '.third.a', 'A')
def third_a():
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
details_route = 'third_a_details'
return render_template('index.html',
context={'ids': ids, 'details_route': details_route}
)

@app.route('/third/b')
@menu.register_menu(app, '.third.b', 'B')
def third_b():
return render_template('index.html')

@app.route('/third/a/<int:id>')
@menu.register_menu(app, '.third.a.details', 'Details', id=id)
def third_a_details(id):
headline = f'Details of Third A id: {id}'
details = f'Here are more details for Third A id: {id}'
return render_template('index.html',
context={'headline': headline, 'details': details}
)

if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
90 changes: 90 additions & 0 deletions examples/menu_and_breadcrumbs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!doctype html>
<html lang="en">
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</head>
<body style="padding-top: 4rem; padding-left: 1rem">

<nav class="navbar navbar-expand-md fixed-top navbar-dark bg-dark justify-content-center">
<div class="container-fluid">
<a class="navbar-brand" href="#">Flask-Menu-App</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item">
<a class="nav-link{% if current_menu.active %} active{% endif %}" aria-current="page" href="{{ current_menu.url }}">{{ current_menu.text }}</a>
</li>
{%- for item in current_menu.children -%}
{% if item.dropdown %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle{% if item.active %} active{% endif %}" data-bs-toggle="dropdown" href="#">{{ item.text }}</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink">
<li><a class="dropdown-item {% if item.active and not item.has_active_child %} active{% endif %}" href="{{ item.url }}">{{ item.text }} List</a>
{%- for subitem in item.children -%}
{% if subitem.url and subitem.hide_on_menu!=True %}
<li><a class="dropdown-item {% if subitem.active %} active{% endif %}" href="{{ subitem.url }}">{{ subitem.text }}</a>
{% endif %}
{%- endfor -%}
</ul>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link{% if item.active %} active{% endif %}" href="{{ item.url }}">{{ item.text }}</a>
</li>
{% endif %}
{%- endfor -%}
</ul>
</div>
</div>
</nav>

<main class="container-fluid">
<nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
<ol class="breadcrumb">
{%- for breadcrumb in current_menu.list_path(current_menu.root, current_menu.active_item.path) -%}
<li class="breadcrumb-item" aria-current="page">
{% if not loop.last %}
<a href="{{ breadcrumb.url }}">{{ breadcrumb.text }}</a>
{% else %}
{% if context and context.headline %}
{{ context.headline }}
{% else %}
{{ breadcrumb.text }}
{% endif %}
{% endif %}
</li>
{%- endfor -%}
</ol>

{% if context and context.ids %}
<div class="card text-dark bg-light mb-3" style="max-width: 30rem;">
<div class="card-header">List of items</div>
<div class="card-body">
<ul>
{% for id in context.ids %}
<li>
<a href="{{ url_for(context.details_route, id=id)}}">Show details of item {{ id }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}


{% if context and context.details %}
<div class="card text-white bg-success mb-3" style="max-width: 30rem;">
<div class="card-header">Details:</div>
<div class="card-body">
<p class="card-text">{{ context.details }}</p>
</div>
</div>
{% endif %}

</main>
</body>
</html>
9 changes: 8 additions & 1 deletion flask_menu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ def url(self):
if key in self._expected_args:
args[key] = g._menu_kwargs[key]

return url_for(self._endpoint, **args)
# url for endpoint with matching number of expected_args
if len(args) == len(self._expected_args):
return url_for(self._endpoint, **args)
return ''

@property
def active(self):
Expand Down Expand Up @@ -381,6 +384,10 @@ def _register_menu_item():
visible_when=visible_when,
expected_args=expected,
**kwargs)

# store path for breadcrumb functionality
item.path = path

return f

return menu_decorator
Expand Down