eZ Documentation Center

This is outdated documentation made for eZ Publish Platform 5.2. It is being moved into the eZ Publish 5.x documentation, so please go there for most up-to-date documentation.

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
titleVersion compatibility

This recipe is compatible with eZ Publish 5.2 / 2013.11

Table of Contents


When listing content (e.g. blog posts), pagination is a very common use case and is usually painful to implement by hand.

For this purpose eZ Publish 5 recommends the use of Pagerfanta library and proposes adapters for it.


Code Block
namespace Acme\TestBundle\Controller;
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchAdapter;
use Pagerfanta\Pagerfanta;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ContentTypeIdentifier;
use eZ\Publish\API\Repository\Values\Content\Query;
class DefaultController extends Controller
    public function myContentListAction()
        // First build the search query.
        // Let's search for folders, sorted by publication date.
        $query = new Query();
        $query->criterion = new ContentTypeIdentifier( 'folder' );
        $query->sortClauses = array( new SortClause\DatePublished() );
        // Initialize the pager.
        // We pass the ContentSearchAdapter to it.
        // ContentSearchAdapter is built with your search query and the SearchService.
        $pager = new Pagerfanta( 
            new ContentSearchAdapter( $query, $this->getRepository()->getSearchService() ) 
        // Let's list 2 folders per page, even if it doesn't really make sense ;-)
        $pager->setMaxPerPage( 2 );
        // Defaults to page 1 or get "page" query parameter
        $pager->setCurrentPage( $this->getRequest()->get( 'page', 1 ) );
        return $this->render(
                'totalFolderCount' => $pager->getNbResults(),
                'pagerFolder' => $pager
Code Block
{% extends noLayout ? viewbaseLayout : "AcmeTestBundle::pagelayout.html.twig" %}
{% block content %}
    <h1>Listing folder content objects: {{ totalFolderCount }} objects found.</h1>
        {# Loop over the page results #}
        {% for folder in pagerFolder %}
            <li>{{ ez_content_name( folder ) }}</li>
        {% endfor %}
    {# Only display pagerfanta navigator if needed. #}
    {% if pagerFolder.haveToPaginate() %}
    <div class="pagerfanta">
        {{ pagerfanta( pagerFolder, 'twitter_bootstrap_translated', {'routeName': location} ) }}
    {% endif %}
{% endblock %}

For more information and examples, have a look at PagerFanta documentation.


Adapter class nameDescription
Makes the search against passed Query and returns Content objects.
Same as ContentSearchAdapter but returns instead SearchHit objects.