Blooki
Grow what you know
Home
>
development
>
Pagination in Blooki
Title
Modification Date
Text
"Shelley has been writing":http://weblog.burningbird.net/archives/2004/09/20/some-gratuitous-weblog-software-writing/ about upcoming Wordpress 1.3 release and one of the features that will be included in the release -- pagination. I got curious about how difficult it would be to add something like this to Blooki. First, I added two template variables (<code>entries::prev</code> and <code>entries::next</code>) that calculate number of entries to display and URL to use. The rest was easy: update template to provide default parameters for number of entries listed and starting position: <pre><code>TemplateVar page::entries <<. $entries{ start => $request{args}{start} || 1, # start from 1 by default display => $request{args}{display} || 20, # display no more than 20 entries header => $request{isentrypage} ? $entry::prevnext : '', # show entry prev/next footer => q[$entries::prevnext], # show entries prev/next if necessary } . </code></pre> This code checks if <code>start</code> and <code>display</code> parameters from query string available or uses 1 and 20 by default; it also sets footer to the value of <code>$entries::prevnext</code> template variable, which is defined as follows: <pre><code>TemplateVar entries::prevnext <<. <div class="prevnext">$get{join ' | ', grep {length} $entries::prev ? qq[<a href="$entries::prev{url}"><< Prev $entries::prev{display}</a>] : '', $entries::next ? qq[<a href="$entries::next{url}">Next $entries::next{display} >></a>] : '', }</div> . </code></pre> This code generates prev/next links. As parameters are not hardcoded anywhere, users can check <code>start</code> and <code>display</code> values as they need (<code>start</code> can even be negative; in this case it will display entries from the end of the list, just like <code>@foo[-2..-1]</code> does). Excited by the ease of the change I also tried to do prev/next links on entries. This didn't required any coding at all; only a small template change: <pre><code>TemplateVar entry::prevnext <<. <div class="prevnext">$get{join ' | ', grep {length} $entries{display => 1, sort => "-modified_on", modified_on => "<$entry{modified_on}", q[<a href="$entry::permalink"><< $entry{title}</a>]}, $entries{display => 1, sort => "+modified_on", modified_on => ">$entry{modified_on}", q[<a href="$entry::permalink">$entry{title} >></a>]}, }</div> . </code></pre> This code displays prev/next links with titles of entries (if there is previous/next entry). It's also easy to make it display prev/next entries in a particular category, just add a category filter <code>category => $entry{category}</code>: <pre><code>TemplateVar entry::prevnext <<. <div class="prevnext">$get{join ' | ', grep {length} $entries{display => 1, category => $entry{category}, sort => "-modified_on", modified_on => "<$entry{modified_on}", q[<a href="$entry::permalink"><< $entry{title}</a>]}, $entries{display => 1, category => $entry{category}, sort => "+modified_on", modified_on => ">$entry{modified_on}", q[<a href="$entry::permalink">$entry{title} >></a>]}, }</div> . </code></pre>