File Coverage

blib/lib/Template/Plugin/Page.pm
Criterion Covered Total %
statement 20 25 80.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 6 7 85.7
pod 2 2 100.0
total 30 39 76.9


line stmt bran cond sub pod time code
1             package Template::Plugin::Page;
2              
3 1     1   81676 use strict;
  1         3  
  1         48  
4 1     1   7 use vars qw($VERSION);
  1         2  
  1         48  
5 1     1   827 use Data::Page;
  1         7699  
  1         10  
6 1     1   878 use Template::Plugin;
  1         681  
  1         32  
7 1     1   6 use base qw(Template::Plugin);
  1         2  
  1         255  
8              
9             # This is incremented every time there is a change to the API
10             $VERSION = '0.10';
11              
12             =head1 NAME
13              
14             Template::Plugin::Page - a plugin to help when paging through sets of results
15              
16             =head1 SYNOPSIS
17              
18             [% USE page = Page($total_entries, $entries_per_page, $current_page) %]
19              
20             First page: [% page.first_page %]
21             Last page: [% page.last_page %]
22             First entry on page: [% page.first %]
23             Last entry on page: [% page.last %]
24              
25             =head1 DESCRIPTION
26              
27             When searching through large amounts of data, it is often the case
28             that a result set is returned that is larger than we want to display
29             on one page. This results in wanting to page through various pages of
30             data. The maths behind this is unfortunately fiddly, hence this
31             module.
32              
33             The main concept is that you pass in the number of total entries, the
34             number of entries per page, and the current page number. You can then
35             call methods to find out how many pages of information there are, and
36             what number the first and last entries on the current page really are.
37              
38             It would be typically used in the following way for an HTML template:
39              
40             [% USE page = Page(134, 10, 13) %]
41             Matches [% page.first %] - [% page.last %] of
42             [% page.total_entries %] records.
43              
44             Page [% page.current_page %] of
45             [% page.last_page %]
46              
47             [% IF page.previous_page %]
48             Previous
49             [% END %]   
50              
51             [% IF page.next_page %]
52             Next
53             [% END %]
54              
55             ... which would output something like:
56              
57             Matches 121 - 130 of
58             134 records.
59              
60             Page 13 of
61             14
62              
63             Previous
64                
65              
66             Next
67              
68             Note that this module is simply a plugin to the Data::Page module.
69              
70             =head1 METHODS
71              
72             =head2 Page
73              
74             This is the constructor. It currently takes two mandatory arguments,
75             the total number of entries and the number of entries per page. It
76             also optionally takes the current page number (which defaults to 1).
77              
78             [% USE page = Page(total_entries, entries_per_page, current_page) %]
79              
80             [%# or #%]
81              
82             [% USE page = Page(134, 10) %]
83              
84             [%# or #%]
85              
86             [% USE page = Page(134, 10, 5) %]
87              
88             =cut
89              
90             sub new {
91 24     24 1 139849 my ($proto, $context, $total_entries, $entries_per_page, $current_page) = @_;
92 24   33     178 my $class = ref($proto) || $proto;
93              
94 24 50       77 ($total_entries, $entries_per_page, $current_page) = ($context, $total_entries, $entries_per_page)
95             unless ref($context) eq 'Template::Context';
96              
97 24         130 my $page = Data::Page->new(
98             $total_entries,
99             $entries_per_page,
100             $current_page);
101              
102 24         1164 return $page;
103             }
104              
105              
106             =head2 total_entries
107              
108             This method returns the total number of entries.
109              
110             Total entries: [% page.total_entries %]
111              
112             =head2 entries_per_page
113              
114             This method returns the total number of entries per page.
115              
116             Entries per page: [% page.entries_per_page %]
117              
118             =head2 current_page
119              
120             This method returns the current page number.
121              
122             Current page: [% page.current_page %]
123              
124             =head2 first_page
125              
126             This method returns the first page. This is put in for reasons of
127             symmetry with last_page, as it always returns 1.
128              
129             Pages range from: [% page.first_page %]
130              
131             =head2 last_page
132              
133             This method returns the total number of pages of information.
134              
135             Pages range to: [% page.last_page %]
136              
137             =head2 first
138              
139             This method returns the number of the first entry on the current page.
140              
141             Showing entries from: [% $page.first %]
142              
143             =head2 last
144              
145             This method returns the number of the last entry on the current page.
146              
147             Showing entries to: [% page.last %]
148              
149             =head2 previous_page
150              
151             This method returns the previous page number, if one exists. Otherwise
152             it returns undefined.
153              
154             Previous page number: [% page.previous_page %]
155              
156             =head2 next_page
157              
158             This method returns the next page number, if one exists. Otherwise
159             it returns undefined.
160              
161             Previous page number: [% page.previous_page %]
162              
163             =head2 splice
164              
165             This method takes in an listref, and returns only the values which are
166             on the current page.
167              
168             [% visible_holidays = page.splice(holidays) %]
169              
170             =cut
171              
172             sub splice {
173 0     0 1   my $self = shift;
174 0           my @values = @{(shift)};
  0            
175              
176 0           @values = splice(@values, $self->first - 1, $self->entries_per_page);
177              
178 0           return @values;
179             }
180              
181              
182             =head1 AUTHOR
183              
184             Leon Brocard,
185              
186             =head1 COPYRIGHT
187              
188             Copyright (C) 2000-2, Leon Brocard
189              
190             This module is free software; you can redistribute it or modify it
191             under the same terms as Perl itself.
192              
193             =cut
194              
195              
196              
197             1;