简体   繁体   中英

Customising the layout of Pagerfanta pagination with a custom template

I have Pagerfanta installed and working, however I am having difficulty in customising the layout. I read on Github that I need to pass through my_template , however I am unsure where this should be configured and what specificially this refers to.

Custom template

If you want to use a custom template, add another argument

<div class="pagerfanta">
    {{ pagerfanta(my_pager, 'my_template') }}
</div>

Ideally I would like to have my own Twig template that I can modify, however I don't know if Pagerfanta supports this. Is it all done in PHP?

I don't think it supports Twig templates but for sure you can write your custom Template class to render the pagination however you want.

Let's say in your AppBundle, you will need to create MyCustomTemplate class which should extend Pagerfanta\\View\\Template\\DefaultTemplate :

<?php

namespace Acme\AppBundle\Template;

use Pagerfanta\View\Template\DefaultTemplate;

class MyCustomTemplate extends DefaultTemplate
{
    // override whatever you need here ...
}

then register it in your services.yml file together with the view service:

services:
    acme_app.template.my_template:
        class: Acme\AppBundle\Template\MyCustomTemplate

    pagerfanta.view.my_template:
        class: Pagerfanta\View\DefaultView
        public: false
        arguments:
            - "@acme_app.template.my_template"
        tags: [{ name: pagerfanta.view, alias: my_template }]

then in your Twig templates you will be able to use:

{{ pagerfanta(my_pager, 'my_template') }}

which will result in displaying your custom pagination template.

my_template is the alias of your view. The next section in the Github link you have provided explains more.

It would look something like this

services:
    app.view.my_template:
        class: App\View\MyView
        public: false
        tags: [{ name: pagerfanta.view, alias: my_template }]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM