Skip to content

Instantly share code, notes, and snippets.

@ceekz
Created Nov 8, 2013
Embed
What would you like to do?
Pagination Helper : Mojolicious + Data::Pageset + Bootstrap 3
use Data::Pageset;
$self->helper(
pagination => sub {
my ($self, $total_entries, $current_page) = @_;
my $dp = Data::Pageset->new({
'total_entries' => $total_entries,
'entries_per_page' => 20,
'current_page' => (! $current_page || $current_page < 1) ? 1 : $current_page,
'pages_per_set' => 10,
'mode' => 'slide',
});
my $html = '<ul class="pagination">';
if ($dp->previous_page) {
$html .= sprintf qq!<li><a href="%s">&laquo;&laquo;</a></li>!, $self->url_with->query([page => undef]);
$html .= sprintf qq!<li><a href="%s">&laquo;</a></li>!, $self->url_with->query([page =>$dp->previous_page]);
} else {
$html .= '<li><span>&laquo;&laquo;</span></li>';
$html .= '<li><span>&laquo;</span></li>';
}
foreach my $p (@{$dp->pages_in_set()}) {
$html .= sprintf qq!<li%s><a href="%s">%d</a></li>!, ($p == $dp->current_page()) ? ' class="active"' : '', $self->url_with->query([page => $p]), $p;
}
if ($dp->next_page) {
$html .= sprintf qq!<li><a href="%s">&raquo;</a></li>!, $self->url_with->query([page => $dp->next_page]);
$html .= sprintf qq!<li><a href="%s">&raquo;&raquo;</a></li>!, $self->url_with->query([page => $dp->last_page]);
} else {
$html .= '<li><span>&raquo;</span></li>';
$html .= '<li><span>&raquo;&raquo;</span></li>';
}
$html .= '</ul>';
return b($html);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment