symfony: page navigation links

by prettyscripts on 2009-07-10 11:55:07 • Leave a comment »

phpsymfony

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('&laquo; First'$uri '1');
    $pages[] = link_to('&lt; 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 &gt;'$uri.$pager->getNextPage());
    $pages[] = link_to('Last &raquo;'$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

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)