2

I use KnpPaginatorBundle to manage pagination in the backoffice of my site and it works well

I configured it to use a custom templates

knp_paginator:
    template:
        pagination: MyappMainBundle::pagination.html.twig

Then, I want to use the same bundle in the frontoffice but with different html structure. So, my question is : Is it possible to have two templates for the pagination? One for the backoffice and One for the frontoffice.

Thanks in advance for your answer.

Matteo
  • 37,680
  • 11
  • 100
  • 115
Mario Radomanana
  • 1,698
  • 1
  • 21
  • 31

1 Answers1

7

You can customise the pagination template in the controller:

$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($target, $page);
$pagination->setTemplate('MyBundle:Pagination:pagination.html.twig');
$pagination->setSortableTemplate('MyBundle:Pagination:sortable.html.twig');

In the View:

{% do pagination.setTemplate('MyBundle:Pagination:pagination.html.twig') %}

or (better) in the render method:

{{ knp_pagination_render(pagination, 'MyBundle:Pagination:pagination.html.twig') }}

More info in the doc of the bundle

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • 1
    Thanks :) ! I didn't see this part of doc – Mario Radomanana Jul 16 '15 at 08:34
  • Hi @MarioJohnathan you are welcome! Which method you applied? – Matteo Jul 16 '15 at 08:42
  • 1
    I used the **{% do pagination %}** in the twig template because I used to do the same with forms via the method [{% form_theme %}](http://symfony.com/doc/current/cookbook/form/form_customization.html#method-1-inside-the-same-template-as-the-form) . That way, I can also reuse "knp_pagination_render" several times in the same file without calling the template multiple times – Mario Radomanana Jul 16 '15 at 08:50
  • 2
    Alternatively you could extend the `Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationExtension` with your own twig functions like `acme_pagination_render_backoffice()` that then calls the `return $this->render($paginator, '::backoffice_paginator.html.twig', $queryParam, $viewParams)` method with the templates already set. That way you wouldn't need to set the template on each usage of the renderer paginator. – qooplmao Jul 16 '15 at 08:59
  • @Qoop Thanks for this tip :) – Mario Radomanana Jul 17 '15 at 06:08