Last active
October 27, 2022 03:07
-
-
Save allhailwesttexas/8c7fe8f8b53190c2ad8a to your computer and use it in GitHub Desktop.
Flask/Jinja2 macro for rendering pagination in a template with Bootstrap components. Can center the component by wrapping in <nav class="text-center">.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro render_pagination(pagination, endpoint) %} | |
<ul class="pagination"> | |
{% if pagination.has_prev %} | |
<li> | |
<a href="{{ url_for(endpoint, page=pagination.prev_num) }}" aria-label="Previous"> | |
<span aria-hidden="true">«</span> | |
</a> | |
</li> | |
{% endif %} | |
{% for p in pagination.iter_pages(left_edge=1, left_current=2, right_current=3, right_edge=1) %} | |
{% if p %} | |
{% if p != pagination.page %} | |
<li> | |
<a href="{{ url_for(endpoint, page=p) }}">{{ p }}</a> | |
</li> | |
{% else %} | |
<li class="active"> | |
<a href="{{ url_for(endpoint, page=p) }}">{{ p }}</a> | |
</li> | |
{% endif %} | |
{% else %} | |
<li class="disabled"> | |
<span class="ellipsis">…</span> | |
</li> | |
{% endif %} | |
{% endfor %} | |
{% if pagination.has_next %} | |
<li> | |
<a href="{{ url_for(endpoint, page=pagination.next_num) }}" aria-label="Next"> | |
<span aria-hidden="true">»</span> | |
</a> | |
</li> | |
{% endif %} | |
</ul> | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how about url is constructed as
url_for('index', tag='tag', page=page)