helloflask / bootstrap-flask

Bootstrap 4 & 5 helper for your Flask projects.
https://bootstrap-flask.readthedocs.io
Other
1.12k stars 190 forks source link

render_nav_item macro missing a **kwargs argument? #203

Closed smontanaro closed 2 years ago

smontanaro commented 2 years ago

If there is a better place to ask for help with bootstrap-flask, let me know. I saw nothing in the docs. BTW, the "this tutorial" link on the PyPI page returns 404: https://bootstrap-flask.readthedocs.io/en/latest/migrate.html.

I'm trying to use bootstrap-flask 2.0.1 (and Bootstrap) for the first time. I'm failing miserably. I have this nav bar Jinja2 code:

{% block nav %}
    {% from 'bootstrap4/nav.html' import render_nav_item %}
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="navbar-nav mr-auto">
            {% for (target, text) in nav %}
                {{ render_nav_item(target, text) }}
            {% endfor %}
        </div>
    </nav>
{% endblock %}

I don't see how to pass the kwargs in that the underlying call to url_for requires. I'm pretty new to Jinja, so I'm quite possibly missing something, but doesn't the macro definition require a **kwargs formal argument?

{% 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 %}<li class="nav-item">{% endif %}
    <a class="{% if not use_li %}nav-item {% endif %}nav-link{% if active %} active" aria-current="page{% endif %}"
       href="{{ url_for(endpoint, **kwargs) }}">
        {{ text }} {% if badge %}<span class="badge badge-light">{{ badge }}</span>{% endif %}
    </a>
    {% if use_li %}</li>{% endif %}
{% endmacro %}

Am I misunderstanding how to pass arguments to render_nav_item()?

greyli commented 2 years ago

Hi, for future questions, welcome to post them to the discussions page.

You can just pass your keyword arguments to render_nav_item, Jinja will collect them and pass them to the underly url_for call (you can access the kwargs dict inside a Jinja macro directly), check out the docs for more details.

I'm planning to remove the badge and use_li parameters or rename them to _badge/_use_li, so they won't shadow user's keyword arguments.

The bad link was fixed in https://github.com/greyli/bootstrap-flask/commit/43585644585a05d9e72f067851063aae66b6ac94, thanks!