Code and Hacks

Stuff I've stumbled on or figured out... mostly Perl, Linux, Mac and Cygwin.

My Photo
Name:
Location: CA, United States

Perl hacker, investor and entrepreneur.

Friday, June 5, 2009

Get Lazy, Use Data::Pageset::Render

I have been using the very nice Data::Pageset module for a while now. It makes separating you data into multiple pages very simple, and for very large datasets it has the slide mode which helps keep your pager small (rather than have links to 100 pages, you get links to the first and last page and the five pages around your current page).

Eventually, I got tired of recreating the same html code for each new pager. I first put together a simple Mason component, but found myself copying it between projects and starting to write one for TT. Eventually, I wrote Data::Pageset::Render. The module (which is on CPAN) subclasses Data::Pageset and adds the html method, which returns the html code, complete with links, to create your pager.

Just create your pager object as you would with Data::Pageset adding link_format => '<a href="http://www.blogger.com/q?page=%p">%a</a>' to the constructor, the html method then eliminates all that redundant paging html.

 my $pager_html = $pager->html();
 # $pager_html is html "<< 1 ... 3 4 5 6 7 ... 10 >>" with appropriate links

 # A bit more control over the appearence of the current page:
 my $pager_html = $pager->html( '<a href="http://www.blogger.com/q?page=%p">%a</a>', '[%a]' );
 # $pager_html is html "<< 1 ... 3 4 [5] 6 7 ... 10 >>" with appropriate links

This works great within a larger framework like TT or Mason:

 # In a TT template all the paging html is reduced to just:
 [% pager.html() %]

 # or in a Mason template:
 <% $pager->html() %>

There aren't very many configuration options now. If there is interest I might make some of the controls customizable (ie, the >> to move forward). Any suggestions are more than welcome.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home