Implement paging
This commit is contained in:
parent
1eed8adf61
commit
00922379c6
1
cpanfile
1
cpanfile
|
@ -3,3 +3,4 @@ requires 'Mojo::File';
|
||||||
requires 'Test::Mojo';
|
requires 'Test::Mojo';
|
||||||
requires 'Mojolicious::Lite';
|
requires 'Mojolicious::Lite';
|
||||||
requires 'Mojo::Pg';
|
requires 'Mojo::Pg';
|
||||||
|
requires 'Mojolicious::Plugin::TagHelpers::Pagination';
|
||||||
|
|
|
@ -11,6 +11,7 @@ use Data::Dumper; # Uncomment for debugging
|
||||||
|
|
||||||
# Plugins
|
# Plugins
|
||||||
plugin 'Config';
|
plugin 'Config';
|
||||||
|
plugin 'TagHelpers::Pagination';
|
||||||
|
|
||||||
# Helpers
|
# Helpers
|
||||||
helper pg => sub {
|
helper pg => sub {
|
||||||
|
@ -38,9 +39,21 @@ app->pg->migrations->from_dir('migrations')->migrate(1);
|
||||||
|
|
||||||
# Routes
|
# Routes
|
||||||
get '/' => sub ($c) {
|
get '/' => sub ($c) {
|
||||||
|
my $max_posts = 5;
|
||||||
my $posts = $c->message->get_posts();
|
my $posts = $c->message->get_posts();
|
||||||
|
my $last_page = sprintf('%d', scalar(@$posts) / $max_posts) + 1;
|
||||||
|
my $this_page = $c->param('page') || $last_page;
|
||||||
|
my $last_post = $this_page * $max_posts - 1;
|
||||||
|
my $first_post = $last_post - $max_posts + 1;
|
||||||
|
my @view_posts = grep defined, @$posts[$first_post..$last_post];
|
||||||
|
|
||||||
$c->render(posts => $posts);
|
$c->stash(
|
||||||
|
view_posts => \@view_posts,
|
||||||
|
this_page => $this_page,
|
||||||
|
last_page => $last_page
|
||||||
|
);
|
||||||
|
|
||||||
|
$c->render();
|
||||||
} => 'index';
|
} => 'index';
|
||||||
|
|
||||||
any '/sign' => sub ($c) {
|
any '/sign' => sub ($c) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
% layout 'default';
|
% layout 'default';
|
||||||
% title 'Home';
|
% title 'Home';
|
||||||
<h2>Messages from the World Wide Web</h2>
|
<h2>Messages from the World Wide Web</h2>
|
||||||
<% for my $row (reverse @$posts) { %>
|
<% for my $row (reverse @$view_posts) { %>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Date:</th>
|
<th>Date:</th>
|
||||||
|
@ -18,3 +18,4 @@
|
||||||
</table>
|
</table>
|
||||||
<br>
|
<br>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
<%= pagination($this_page, $last_page, '?page={page}') %>
|
||||||
|
|
Loading…
Reference in a new issue