Skip to content

Instantly share code, notes, and snippets.

@allhailwesttexas
Last active March 22, 2025 10:24
Show Gist options
  • Select an option

  • Save allhailwesttexas/8c7fe8f8b53190c2ad8a to your computer and use it in GitHub Desktop.

Select an option

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)

@hammadfareedH14
Copy link

please tell macro render kiya ha work as like as import statemnet plaes tell me about something in short and effective detail

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