[Catalyst] Re: OT: Better TT pager?

David Schmidt davewood at gmx.at
Thu Jan 22 12:54:53 GMT 2009


On Thu, Dec 25, 2008 at 12:18 AM, Aristotle Pagaltzis <pagaltzis at gmx.de> wrote:
> * Jesse Sheidlower <jester at panix.com> [2008-12-23 12:45]:
>> Does someone have a model I can steal from?
>
> I have something pretty close to that. Not entirely happy insofar
> as that I want to add the ability show a few links to skip 10 and
> 20 pages as appropriate, and it's not abstracted as it should be,
> but as a starting point it should work.
>
>    USE decimal = format('%d') ;
>    BLOCK pagination ;
>      IF NOT page ; page = 0 ; END ;
>      last_page = decimal( ( num_trials - 1 ) / per_page );
>
>      IF last_page ;
>        %]<div class="pagination">[%
>        %]<a href="[% c.req.uri_with( page = [] ) %]"[% ' class="current"' IF NOT page %]>1</a>[%
>
>        linked_page = page - 4 ;
>        IF linked_page < 1 ; linked_page = 1 ; END ;
>
>        until_page = linked_page + 7 ;
>        IF last_page <= until_page ;
>          until_page = last_page - 1 ;
>          linked_page = until_page - 7 ;
>          IF linked_page < 1 ; linked_page = 1 ; END ;
>        END ;
>
>        '<i>&#x2026;</i>' IF 1 < linked_page ;
>
>        WHILE ( linked_page <= until_page ) AND ( linked_page < last_page ) ;
>          %]<a href="[% c.req.uri_with( page = linked_page ) %]"[% ' class="current"' IF page == linked_page %]>[% linked_page + 1 %]</a>[%
>          linked_page = linked_page + 1 ;
>        END ;
>
>        '<i>&#x2026;</i>' IF linked_page < last_page ;
>        %]<a href="[% c.req.uri_with( page = last_page ) %]"[% ' class="current"' IF page == last_page %]>[% last_page + 1 %]</a>[%
>
>        %]</div>[%
>      END ;
>    END ;
>
> This is also a perfect demonstration of why I say that TT sucks.
> This should by all rights be handled in the template since it's
> display logic, but writing it in that TT mini-language rather
> than Perl is fugly. And PERL and RAWPERL blocks have their own
> problems. Anyway. Someday I'll quit yapping and write some code.
>
> The CSS for styling that looks like this:
>
>    div.pagination {
>        margin: 1.1em 0 0.5em;
>        text-align: right;
>        padding: 0.2em 0;
>    }
>
>    div.pagination i {
>        font-style: normal;
>    }
>
>    div.pagination a:link,
>    div.pagination a:visited {
>        border: 1px solid #359;
>        background-color: #f0f0e6;
>        padding: 0.1em 0.3em;
>        margin: 0 0.2em;
>    }
>
>    div.pagination a.current,
>    div.pagination a:focus,
>    div.pagination a:hover,
>    div.pagination a:active {
>        background-color: #47b;
>        color: #fff;
>    }
>
> I'm not sure how selfcontained that is, but I can't be bothered
> to dig through the rest of my stylesheets right now.
>
> Regards,
> --
> Aristotle Pagaltzis // <http://plasmasturm.org/>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>

I adapted one of the proposals and felt I should share it with you.

in my Controller:
sub index :Path :Args(0) {
    my ( $self, $c ) = @_;
    my $page = $c->req->param('page') || 1;
    my $rows  = $c->req->param('rows') || 3;
    my $where = { };
    my $attr = { page => $page, rows => $rows, order_by => "created desc" };
    my $rs = $c->model('DB::Pictures')->search( $where, $attr );
    my $pager = $rs->pager();
    $c->stash->{pager} = $pager;
    $c->stash->{pictures} = [$rs->all];
    $c->stash->{template} = 'pictures/list.tt2';
}


in my template.tt2:
</div>
[% IF pager.current_page() != pager.first_page() -%]
    <a href="[% c.request.uri_with( page => pager.first_page() )
-%]">&lt;&lt;</a>
[% END -%]

[% IF pager.previous_page -%]
    <a href="[% c.request.uri_with( page => pager.previous_page() )
-%]"> &lt;</a>
[% END -%]

[% IF pager.next_page -%]
    <a href="[% c.request.uri_with( page => pager.next_page() ) -%]"> &gt;</a>
[% END -%]

[% IF pager.current_page != pager.last_page() -%]
    <a href="[% c.request.uri_with( page => pager.last_page() )-%]">
&gt;&gt;</a>
[% END -%]
</div>
<div>
[% IF pager.total_entries() > 0 -%]
    [% pager.total_entries() -%] pictures found |
    Showing page <b>[% pager.current_page() -%]</b> of [% pager.last_page() -%]
[% END -%]
</div>

Works fine so far.

david

-- 
David Schmidt   |   http://www.fm5.at



More information about the Catalyst mailing list