Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Minimal CSS pagination helper

This is helper is mostly inspired by Pagination navigation helper, but there are some differences:

PaginationHelper.php

function pagination($pager)
{
    $uri = sfRouting :: getInstance()->getCurrentInternalUri();
    $html = '';
 
    if ($pager->haveToPaginate())
    {
        $uri .= strstr($uri, '?') ? '&page=' : '?page=';
 
        if ($pager->getPage() != 1)
        {
            $html .= '<li>' . link_to('first', $uri . '1') . '</li>';
            $html .= '<li>' . link_to('previous', $uri . $pager->getPreviousPage()) . '</li>';
        }
 
        foreach ($pager->getLinks() as $page)
        {
            if ($page == $pager->getPage())
                $html .= '<li><strong>' . link_to($page, $uri . $page) . '</strong></li>';
            else
                $html .= '<li>' . link_to($page, $uri . $page) . '</li>';
        }
 
        if ($pager->getPage() != $pager->getLastPage())
        {
            $html .= '<li>' . link_to('next', $uri . $pager->getNextPage()) . '</li>';
            $html .= '<li>' . link_to('last', $uri . $pager->getLastPage()) . '</li>';
        }
 
        $html = '<ul class="pagination">' . $html . '</ul>';
    }
 
    return $html;
}

Minimal CSS

ul.pagination li {
    display: inline;
    list-style-type: none;
    padding-right: 1em;
}

In your template

<?php echo use_helper('Pagination') ?>
<?php echo pagination($pager) ?>
by brikou on 2006-07-19, tagged css  helper  pager  pagination  propel 

Comments on this snippet

gravatar icon
#1 dav pach on 2006-07-19 at 11:58

Maybe you can make a better CSS to display your pagination but it is nice

gravatar icon
#2 Francois Zaninotto on 2006-07-19 at 06:26

I think you could use the link_to_if() helper to display either a link to a page or the sole name of the page without link if it's the current one.

gravatar icon
#3 brikou on 2006-07-20 at 11:53

yes francois, i could change the lines (in the foreach loop) by this one

$html .= '<li>' . link_to_unless ($page == $pager->getPage(), $page, $uri . $page) . '</li>';

but we loose the emphasize (done with strong) of the current page...

gravatar icon
#4 Pierre Minnieur on 2006-09-02 at 01:49

I found a bug, a stupid bug. The "getCurrentInternalUri()" method won't give a correct route name, if you use action forwarding. I'm using it, because I want to fake modules beeing part of another module.

I use it for a nice navigation - I have the area "account management" where I have the modules groups, roles, actions, apps, modules, users - but I want to know if this module is part of the users-array via $sf_first_module, so I wrote a wrapping action executeForward() in the user module, and the routes contain values like forward_module / forward_ation, so the executeForward action just executes this route forwarding routes.

But if I am on a forwarded page, e.g. I'm listening the roles in the database, and I must paginate them on the end of the list, getCurrentInternalUri() won't give me the forwarded route (user_roles_list), instead I get "forward?page=". That's right, the last action/route was the forwarding, but now I can't paginate.

So, instead, try to use the method gettCurrentRouteName() and add a simple '@' in front of it.

<code>$uri = '@'.sfRouting::getInstance()->getCurrentRouteName();</code>

This simply works.

gravatar icon
#5 Pierre Minnieur on 2006-09-02 at 02:03

And I found another problem (omg, it's bug-clean-day!). The route must contain only the page-var, no others, otherwise they won't be put in. So, I added a simple mechanism to add custom route values.

<code>function pagination($pager, $params = array()) { $uri = '@'.sfRouting::getInstance()->getCurrentRouteName(); if (is_array($params) and ($count = count($params)) !== false and $count > 0) { $counter = 0; foreach ($params as $key => $value) { $uri .= (strstr($uri, '?') ? '&' : '?') . $key . '=' . $value; } } ... </code>

That's it. Now you can use it like:

<code><?php echo pagination($users, array('user_role_name' => $list_user_role->getName())) ?></code>

gravatar icon
#6 damien pitard on 2009-07-07 at 01:07

with Symfony 1.2 you need to write <pre> $uri = sfContext::getInstance()->getRouting()->getCurrentInternalUri(); instead </pre> instead of <pre> $uri = sfRouting :: getInstance()->getCurrentInternalUri(); </pre>

You need to create an account or log in to post a comment or rate this snippet.