by prettyscripts on 2009-07-10 11:55:07 • Leave a comment »
pagination helper in the code snippets section of symfony’s website.
the code is modified to include <ul> and <li> tags and to correctly parse uri when passed as routing rule:
PHP:
function page_navigation($pager, $uri) | |
{ | |
$pages = array(); | |
| |
if ($pager->haveToPaginate()) | |
{ | |
$uri .= (preg_match('/\?/', $uri) ? '&' : '?').'page='; | |
| |
// first / prev links | |
$pages[] = link_to('« First', $uri . '1'); | |
$pages[] = link_to('< Prev', $uri . $pager->getPreviousPage()); | |
| |
// pages one by one | |
$links = array(); | |
foreach ($pager->getLinks() as $page) | |
{ | |
$pages[] = link_to_unless($page == $pager->getPage(), $page, $uri.$page); | |
} | |
| |
// next / last links | |
$pages[] = link_to('Next >', $uri.$pager->getNextPage()); | |
$pages[] = link_to('Last »', $uri.$pager->getLastPage()); | |
} | |
| |
return '<ul><li>' . implode('</li><li>', $pages) . '</li></ul>'; | |
} |
save the file as PaginationHelper.php in project/root/lib/helper or apps/application/lib/helper.
edit template file:
Code:
<?php echo use_helper('Pagination') ?> | |
<?php echo pager_navigation($page, '@routing') ?> |
Tags: navigation, page, symfony