Files
core/templates/helpers/_pager_basic.html.twig
2022-06-22 17:26:37 +02:00

71 lines
3.1 KiB
Twig

{#
Predefined variables:
- `records`: The records to iterate over
- `route`: The name of the route, to use in `{{ path() }}`
- `routeParams`: Parameters to pass in to `{{ path() }}` to create the correct url
- `surround`: The amount of items to show around the 'current' one. "3" by default.
- `class`: The main CSS class to apply to the pager. "pagination" by default
- `previous_link_class`: The CSS class to apply to the previous link in the pagination. "previous" by default.
- `next_link_class`: The CSS class to apply to the next link in the pagination. "next" by default.
#}
{% set start = max(records.currentPage - surround, 1) %}
{% set end = min(records.currentPage + surround, records.nbPages) %}
{% block item %}
{% if path is defined %}
<li {% if class|default %}class="{{ class }}"{% endif %} {% if enabled is defined and not enabled %}disabled="disabled"{% endif %}>
<a {% if 'active' in class|default %}aria-current="page"{% endif%} {% if enabled is defined and not enabled %}disabled="disabled"{% else %}href="{{ path }}"{% endif %}>{{ label|default('…') }}</a>
</li>
{% endif %}
{% endblock item %}
{% if records.haveToPaginate|default() %}
<ul class="{{ class }}">
{# Previous Button #}
{% set p = routeParams|merge({page: records.hasPreviousPage ? records.previousPage : 1 }) %}
{% with {'path': path(route, p), 'label': 'pager.previous'|trans, 'enabled': records.hasPreviousPage, 'class': previous_link_class } only %}
{{ block('item') }}
{% endwith %}
{# If we didn't start at the beginning, add a link to the first. #}
{% if start > 1 %}
{% set p = routeParams|merge({page: 1}) %}
{% with {'path': path(route, p), 'label': 1 } %}
{{ block('item') }}
{% endwith %}
{% with { 'path': '', 'label': '…', 'enabled': false } %}
{{ block('item') }}
{% endwith %}
{% endif %}
{# Iterate over the items. #}
{% for i in start .. end %}
{% set p = routeParams|merge({page: i}) %}
{% set class = (i == records.currentPage ? ' active' : '') %}
{% with {'path': path(route, p), 'label': i, 'class': class, 'enabled': true } %}
{{ block('item') }}
{% endwith %}
{% endfor %}
{# If we didn't finish with the last, add a link to the end #}
{% if end < records.nbPages %}
{% with { 'path': '', 'label': '…', 'enabled': false } %}
{{ block('item') }}
{% endwith %}
{% set p = routeParams|merge({page: records.nbPages}) %}
{% with {'path': path(route, p), 'label': records.nbPages } %}
{{ block('item') }}
{% endwith %}
{% endif %}
{% set p = routeParams|merge({page: records.hasNextPage ? records.nextPage : records.nbPages }) %}
{% with {'path': path(route, p), 'label': 'pager.next'|trans, 'enabled': records.hasNextPage, 'class': next_link_class } only %}
{{ block('item') }}
{% endwith %}
</ul>
{% endif %}