Pagination has become kids play with Dancer ...
I published my first blog on 29th Sep'2011 about how Dancer and Rose::DB can be used together to build a web app in no time. Here is the link to that blog, if you have missed it.
I have been thinking of improving on that and first thing that came to my mind is how about adding pagination feature while displaying the items. There are many gems available on CPAN to help you in pagination. However I was looking for something that is easy to plug in with my Dancer web app. In my research I came across an example doing exactly what I was looking, although I find it incomplete by Alexis Sukrieh. Here is the link to that code.
I grabbed the idea from the above example and incorporated into my Dancer web app that I wrote as a part of my first blog. Here is the link to the code, in case you are looking for.
I am going to add this to my public github repository very soon.
Before I jump into details of pagination, let me share my experience with the plugin for pagination Template::Plugin::Pagination v0.90. Looking into source, I found out that this module is simply a wrapper around another module Data::Paginated v1.01 which is derived from Data::Pageset. I also noticed there is an open issue for the module Template::Plugin::Pagination
Having spent some time, I realized it is nothing to do with this Template::Plugin::Pagination but with Data::Paginated. So I raised a ticket for the author of Data::Paginated and provides the solution to the issue. For more information, you can view the ticket here.
Enough of the talking, lets begin the journey of pagination with Dancer. I assume you have the WebApp v0.1 source code, if not, get it here.
First thing first, lets modify the web app configuration file (config.yml) and add the following lines:
engines: template_toolkit: encoding: 'utf8' PLUGIN_BASE: 'Template::Plugin::Pagination'
Next is to modify the template for displaying the items (views/display.tt) by adding the following line just after the <% IF items %>
<% USE page = Pagination(items, current_page, entries_per_page) %>
Also replace 'items' as in with 'page.page_data'
<% FOREACH item IN items %>
to
<% FOREACH item IN page.page_data %>
Once we are done with the all the items, we would want to be able to move around page. For that we will add the link to first, prev, next and last page link. Please refer to the source code as I am unable to display it here for some reason (my lack of knowledge) here.
We are almost there, we just to need pass in the value for current_page and entries_per_page something like below, to the get '/display' in the lib/WebApp.pm
current_page => params->{'page'}, entries_per_page => 5,
For testing purpose, I have used 5 entries per page but you are free to set it to whatever you like.
Please find the source code here used in this blog.
I hope you have enjoyed the Dance with ... :-)
Best Regards,
Mohammad S Anwar
Congratulations on your second post.
I love the fact that Dancer team/people and Mojolicious team/people are competing in a healthy way. Competition is good.
Thank you very much, Steven, for the encouraging words. Unfortunately I am not part of Dancer team/people. However I would love to be one of them and contribute in whatever capacity I get the opportunity.
Best Regards,
Mohammad S Anwar.
You're the kind of user that makes developing Dancer a real treat. :)
I can't believe that Sawyer X, author of Dancer, has such a nice thing to say about me.
Many Thanks.
Best Regards,
Mohammad S Anwar