Skip to content

Instantly share code, notes, and snippets.

@allhailwesttexas
Last active October 27, 2022 03:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allhailwesttexas/8c7fe8f8b53190c2ad8a to your computer and use it in GitHub Desktop.
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">.
{% 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">&laquo;</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">&hellip;</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">&raquo;</span>
</a>
</li>
{% endif %}
</ul>
{% endmacro %}
@binrebin
Copy link

binrebin commented Oct 9, 2020

how about url is constructed as url_for('index', tag='tag', page=page)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment