Crear un paginador en Symfony es una tarea bastante sencilla y que no nos dará quebraderos de cabeza. Solo tenemos que modificar dos ficheros, el de acciones y la template donde aparecerá el paginador. En el fichero actions.class.php añadimos lo siguiente:
public function executeIndex (sfWebRequest $request) { $this->pager = new sfDoctrinePager('Noticias', '5');//Limitamos a 5 el número de contenidos a mostrar $this->pager->setQuery(Doctrine::getTable('Noticias')->createQuery('a')); $this->pager->setPage($request->getParameter('page', 1)); $this->pager->init(); } |
Para este ejemplo tenemos que modificar la template indexSuccess.php:
<!--Mostramos los resultados--> <?php foreach ($pager->getResults() as $noticia): ?> <h2><?php print $noticia->getTitulo() ?></h2><br> <?php print html_entity_decode($noticia->getContenido()) ?><br> <br><br><br> <?php endforeach; ?> <!-- Mostramos el paginador --> <div style="width:5px;float:left;margin-top:3px;margin-right:10px"> <?php print link_to('|<', 'noticias/list?page='.$pager->getFirstPage()) ?> </div> <div> <?php if ($pager->haveToPaginate()): ?> <?php $links = $pager->getLinks(); foreach ($links as $page): ?> <div style="padding:5px 5px 5px 5px;border:#000000 thin solid;float:left;width:10px;margin-left:3px;font-size:10px" > <?php print ($page == $pager->getPage()) ? $page : link_to($page, 'noticias/list?page='.$page) ?> </div> <?php endforeach ?> <?php endif ?> </div> <div style="width:5px;float:left;margin-left:10px;margin-top:3px;"> <?php print link_to('>|', 'noticias/list?page='.$pager->getLastPage()) ?> </div> |