mourningdove/views/components/pagination.tt
2026-05-24 01:03:05 +00:00

84 lines
2.8 KiB
Text

[%# components/pagination.tt
Pagination block. Call as follows:
INCLUDE components/pagination.tt
current => 1, # current page we're viewing
total_pages => 5, # total number of pages this is split into
Authors:
Afuna <coder.dw@afunamatata.com>
Copyright (c) 2015 by Dreamwidth Studios, LLC.
This program is free software; you may redistribute it and/or modify it under
the same terms as Perl itself. For a copy of the license, please reference
'perldoc perlartistic' or 'perldoc perlgpl'.
%]
[%- IF total_pages > 1 -%]
[% opts = {keep_args => 1};
IF cur_args;
opts.cur_args = cur_args;
END %]
<ul class="pagination">
[% IF current > 1 %]
[% opts.args = { page => (current - 1) } %]
<li class="arrow"><a href="[% dw.create_url( path, opts ) %]">&laquo;</a></li>
[% END %]
[% IF total_pages <= 7 %]
[%# If there's less than seven pages, print all of them. %]
[%- INCLUDE print_pages range=[1..total_pages] -%]
[% ELSE %]
[%- # Time for weird math! There are three display possiblities:
# 1) current page is near the start, so we have a big start block and small end
# 2) current page is near the end, so we have a small start and big end
# 3) current page is near neither, so we have a small start and end, and a center block
# each setting will print 7 items - either:
# 1) 5 pages, ellipsis, 1 page
# 2) 1 page, ellipsis, 5 pages
# 3) 1 page, ellipsis, 3 pages, ellipsis, 1 page
end_range_start = total_pages - 4;
start_range_end = 5; # 1 plus 4, as the end is last minus 4
in_end_range = end_range_start < current;
in_start_range = current < start_range_end;
in_center_range = !(in_start_range || in_end_range); # both ends are collapsed
start_range = in_start_range ? [1..start_range_end] : [1];
end_range = in_end_range ? [end_range_start..total_pages] : [ total_pages] ;
center_start = current - 1;
center_end = current + 1;
-%]
[%- INCLUDE print_pages range=start_range -%]
[%- INCLUDE print_ellipsis -%]
[% IF in_center_range %]
[%- INCLUDE print_pages range=[center_start..center_end] -%]
[%- INCLUDE print_ellipsis -%]
[% END %]
[%- INCLUDE print_pages range=end_range -%]
[% END %]
[% IF current < total_pages %]
[% opts.args = { page => (current + 1) } %]
<li class="arrow"><a href="[% dw.create_url( path, opts) %]">&raquo;</a></li>
[% END %]
</ul>
[%- END -%]
[%- BLOCK print_pages -%]
[%- FOREACH page_num = range -%]
[% opts.args = { page => page_num } %]
<li[% IF page_num == current %] class="current" [% END %]><a href="[% dw.create_url( path, opts ) %]">[% page_num %]</a></li>
[%- END -%]
[%- END -%]
[%- BLOCK print_ellipsis -%]
<li class="unavailable"><a href="">&hellip;</a></li>
[%- END -%]