From 8dd904634c75c56fe62590604da2abacda15992e Mon Sep 17 00:00:00 2001 From: Grey Li Date: Mon, 12 Aug 2024 20:35:35 +0800 Subject: [PATCH] Fix the badge classes in render_nav_item macro for Bootstrap 5 --- CHANGES.rst | 8 ++++ docs/macros.rst | 4 +- flask_bootstrap/templates/base/nav.html | 11 ------ flask_bootstrap/templates/bootstrap4/nav.html | 10 +++++ flask_bootstrap/templates/bootstrap5/nav.html | 10 +++++ tests/test_bootstrap5/test_render_nav_item.py | 37 +++++++++++++++++++ 6 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 tests/test_bootstrap5/test_render_nav_item.py diff --git a/CHANGES.rst b/CHANGES.rst index 0f4104a0..51cea37c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,14 @@ Changelog ========= +2.4.1 +----- + +Release date: - + +- Fix the badge classes in the ``render_nav_item`` macro for Bootstrap 5. +- Add ``_badge_classes`` param to the ``render_nav_item`` macro to set badge classes. + 2.4.0 ----- diff --git a/docs/macros.rst b/docs/macros.rst index 5807947e..83bee9b1 100644 --- a/docs/macros.rst +++ b/docs/macros.rst @@ -25,11 +25,13 @@ Example API ~~~~ -.. py:function:: render_nav_item(endpoint, text, _badge='', _use_li=False, **kwargs) +.. py:function:: render_nav_item(endpoint, text, _badge='', _use_li=False, _badge_classes='...', **kwargs) :param endpoint: The endpoint used to generate URL. :param text: The text that will displayed on the item. :param _badge: Badge text. + :param _badge_classes: Badge classes. For Bootstrap 4, the default value is ``badge badge-light``. + For Bootstrap 5, the default value is ``badge text-bg-light``. :param _use_li: Default to generate ````, if set to ``True``, it will generate ``
  • ``. :param kwargs: Additional keyword arguments pass to ``url_for()``. diff --git a/flask_bootstrap/templates/base/nav.html b/flask_bootstrap/templates/base/nav.html index ab0ae338..eb767d84 100644 --- a/flask_bootstrap/templates/base/nav.html +++ b/flask_bootstrap/templates/base/nav.html @@ -1,14 +1,3 @@ -{% macro render_nav_item(endpoint, text, _badge='', _use_li=False) %} - {% set active = True if request.endpoint and request.endpoint == endpoint else False %} - {% if _use_li %}{% endif %} -{% endmacro %} - - {% macro render_breadcrumb_item(endpoint, text) %} {% set active = True if request.endpoint and request.endpoint == endpoint else False %} {% endif %} +{% endmacro %} diff --git a/flask_bootstrap/templates/bootstrap5/nav.html b/flask_bootstrap/templates/bootstrap5/nav.html index 21ee8a4b..94aa0a2f 100644 --- a/flask_bootstrap/templates/bootstrap5/nav.html +++ b/flask_bootstrap/templates/bootstrap5/nav.html @@ -1 +1,11 @@ {% extends 'base/nav.html' %} + +{% macro render_nav_item(endpoint, text, _badge='', _use_li=False, _badge_classes='badge text-bg-light') %} + {% set active = True if request.endpoint and request.endpoint == endpoint else False %} + {% if _use_li %}{% endif %} +{% endmacro %} diff --git a/tests/test_bootstrap5/test_render_nav_item.py b/tests/test_bootstrap5/test_render_nav_item.py new file mode 100644 index 00000000..8c1856ab --- /dev/null +++ b/tests/test_bootstrap5/test_render_nav_item.py @@ -0,0 +1,37 @@ +from flask import render_template_string + + +def test_render_nav_item(app, client): + @app.route('/hello') + def hello(): + return 'Hello' + + @app.route('/nav_item/4') + def test_bootstrap4(): + return render_template_string(''' + {% from 'bootstrap4/nav.html' import render_nav_item %} + {{ render_nav_item('hello', 'Hello Nav', _badge='beta') }} + ''') + + @app.route('/nav_item/5') + def test_bootstrap5(): + return render_template_string(''' + {% from 'bootstrap5/nav.html' import render_nav_item %} + {{ render_nav_item('hello', 'Hello Nav', _badge='beta', _use_li=True) }} + ''') + + response = client.get('/nav_item/4') + data = response.get_data(as_text=True) + assert 'href="/hello">' in data + assert 'span class="badge badge-light"' in data + assert 'beta' in data + assert '' not in data + + response = client.get('/nav_item/5') + data = response.get_data(as_text=True) + assert 'href="/hello">' in data + assert 'span class="badge text-bg-light"' in data + assert 'beta' in data + assert '' in data