diff --git a/.gitignore b/.gitignore index d167d87..ed0a1cb 100644 --- a/.gitignore +++ b/.gitignore @@ -54,4 +54,7 @@ coverage.xml docs/_build/ # PyBuilder -target/ \ No newline at end of file +target/ + +# VSCode +.vscode/ diff --git a/AUTHORS.rst b/AUTHORS.rst index 7dd7dfb..2764afd 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -17,3 +17,4 @@ Contact us at `info@inveniosoftware.org `_ * Sami Hiltunen * Sylvain Boily * Marlin Forbes +* Grzegorz Gyczew diff --git a/examples/menu_and_breadcrumbs/app.py b/examples/menu_and_breadcrumbs/app.py new file mode 100644 index 0000000..7afe65a --- /dev/null +++ b/examples/menu_and_breadcrumbs/app.py @@ -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/') +@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/') +@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/') +@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) \ No newline at end of file diff --git a/examples/menu_and_breadcrumbs/index.html b/examples/menu_and_breadcrumbs/index.html new file mode 100644 index 0000000..3c3d55d --- /dev/null +++ b/examples/menu_and_breadcrumbs/index.html @@ -0,0 +1,90 @@ + + + + + + + + + + +
+
+ + \ No newline at end of file diff --git a/flask_menu/__init__.py b/flask_menu/__init__.py index 3f4ba5f..606e180 100644 --- a/flask_menu/__init__.py +++ b/flask_menu/__init__.py @@ -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): @@ -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